Compare list entry from csv files

Anatoli Hristov tolidtm at gmail.com
Tue Nov 27 09:24:55 EST 2012


On Tue, Nov 27, 2012 at 4:23 AM, Dave Angel <d at davea.name> wrote:
> On 11/26/2012 05:27 PM, Anatoli Hristov wrote:
>> I understand, but in my case I have for sure the field "Name" in the
>> second file that contains at least the first or the last name on it...
>> So probably it should be possible:)
>> The Name "Billgatesmicrosoft" contains the word "Gates" so logically I
>> might find a solution for it.
>>
>
> (Please don't top-post.  Or if you must, then delete everything after
> your post, as I'm doing here.  Otherwise you end up with insanities like
> new stuff, quote-4, quote-1, quote-3, quote-2.  In this case, long
> tradition on this forum and many like it work well, even if Microsoft
> mail programs and some others decide to put the cursor at the wrong end
> of the existing text.  In most programs, it's configurable.)
>
> If you can come up with an algorithm for comparing first+last in one
> file to name in the other, then the problem can be solved.  But you
> can't do it by hand-waving, you have to actually figure out a mechanism.
>  Then we can help you code such a thing.  And I can just about guarantee
> that if these fields are created independently by human beings, that
> there will be exceptions that have to fixed by human beings.
>
>
> --
>
> DaveA

Thanks for your help. I will do my best for the forum :)

I advanced a little bit with the algorithm and at least I can now
extract and compare the fields :)
For my beginner skills I think this is too much for me. Now next step
is to add the second field with the number to the Namelist and copy it
to a third filename I suppose.

import csv

origf = open('c:/Working/Test_phonebook.csv', 'rt')
secfile = open('c:/Working/phones.csv', 'rt')

phonelist = []
namelist = []

names = csv.reader(origf, delimiter=';')
phones = csv.reader(secfile, delimiter=';')
for tel in phones:
    phonelist.append(tel)

#print "*"*25,phonelist,"*"*25
rows = 0

def finder(name_row):
    for ex_phone in phonelist:
#        phonelist.append(tel)
        telstr = ex_phone[0].lower()
#        print "*"*25 + " I got %s" % telstr
#        print "\nGot name from Name_Find :%s" % name_row
        if telstr.find(name_row) >= 0:
            print "\nName found: %s" % name_row
        else:
            pass
    return
#            print "\nNot found %s" % name_row

def name_find():

    for row in names:
        namelist.append(row)
        name_row = row[0].lower()
#        print "\nExtracted Name is :% s" % name_row
        finder(name_row)
#        name_find()
#    rows = rows +1
name_find()



More information about the Python-list mailing list