Compare list entry from csv files

Dave Angel d at
Tue Nov 27 20:59:56 CET 2012

On 11/27/2012 01:57 PM, Anatoli Hristov wrote:
> <SNIP unreferenced information>
> Thank you all for the help, but I figured that out and the program now
> works perfect.


>  I would appreciate if you have some notes about my
> script as I'm noob :)
> Here is the code:
> import csv
> origf = open('c:/Working/Test_phonebook.csv', 'rt')
> secfile = open('c:/Working/phones.csv', 'rt')
I cannot tell which of these files is which, since you use different
names than you did in your example text of the OP.

> phonelist = []
> namelist = []
> names = csv.reader(origf, delimiter=';')
> phones = csv.reader(secfile, delimiter=';')
> for tel in phones:
>     phonelist.append(tel)
Is phonelist the one that has just two fields:  name & phone number?

del phones

Why don't you populate the namelist in the same way, so that it's clear
what's going on ?

del names
Two many globals, being referenced directly in functions.  It's fine to
have them, but if they're const, like PHONELIST, then make them
uppercase.  If they're not, then pass them into the functions where used.
> def finder(name_row,rows):
>     for ex_phone in phonelist:
>         telstr = ex_phone[0].lower()
>         if telstr.find(name_row) >= 0:
So will this logic find a match between a Mr. Jones living in the city
of Townsend and a Mr. Townsend that you have a phone number for?  Or do
I have the files backward, and this will compare the first name from one
file to the phone number from the other?
>             print "\nName found: %s" % name_row
>             namelist[rows][-1] = ex_phone[-1].lower()
                     return   #otherwise, we might find multiple matches
>         else:
>             pass
Need a print message here indicating that no name matched.  Otherwise,
you just think it worked.
>     return
> def name_find():
>     rows = 0
No need to keep your own index, since finder() can just adjust the list
directly, without having to reach into globals.

>     for row in names:
for row_num, row in enumerate(names):   #(if you actually needed such an
>         namelist.append(row)
>         name_row = row[0].lower()
Might this actually be first_name ?  i find it very confusing to store a
string in a variable with the word row in it.
>         finder(name_row,rows)
>         rows = rows+1

All the initialization you had at the beginning belongs here, and it
also belongs inside an  if __name__ == "__main__": clause

Even better, it all belongs in a function, which returns the two lists
you're going to work with.

> name_find()
> ofile  = open('c:/Working/ttest.csv', "wb")
> writer = csv.writer(wfile, delimiter=';')
What is wfile?   Looks undefined to me.
> for insert in namelist:
>     writer.writerow(insert)
> wfile.close()

I really think you need to document the matching pattern you're trying
to implement, and then we can worry about whether the code correctly
implements that algorithm.  Finally, we can worry about clarity of the code.



More information about the Python-list mailing list