Compare list entry from csv files

Dave Angel d at davea.name
Fri Nov 30 04:17:09 CET 2012


On 11/29/2012 05:22 AM, Anatoli Hristov wrote:
> <SNIP>
> Hello,
>
> Tried to document a little bit the script, but I'm not that good in that too :)
>
> The only problem I have is that I cant compare other field than the
> first one in
> for ex_phone in phones:
>         telstr = ex_phone[0].lower()
> When I use telstr = ex_phone[0].lower() it says out of range and the
> strange think is that the range is 6 I can't figure that out. So when
> I edit the csv I modify the look of the file and then I start the
> script and it works, but I wanted to use more than one condition and I
> can't :(
>
>
>
>
> import csv
>
> # Open the file with the names and addresses
> origf = open('c:/Working/vpharma.csv', 'rt')
> # Open the  file with the phone numbers
> secfile = open('c:/Working/navori.csv', 'rt')
>
> # Creates the empty list with the names
> namelist = []
> # Creates the empty list with the phone numbers
> PHONELIST = []
>
>
> # Reads the file with the names
> # Format "Name","Phone"
> names = csv.reader(origf, delimiter=';')
>
> # Reads the file with the phone numbers
> # Format "First name","Lastname","Address","City","Country","Phone"
> phones = csv.reader(secfile, delimiter=';')
>
> # Creates a list with phone numbers
> #for tel in phones:
> #    PHONELIST.append(tel)
Without populating the PHONELIST here, you have a serious problem.  Why
is it commented out?

>
> def finder(Compare_Name,rows):
>     '''
>     Compare the names from the namelist with the names from the phonelist.
>     If the name match - then the phone number is added to the specified field
>     '''
>     for ex_phone in phones:

You should be using PHONELIST here as well.  phones is a pseudo-file,
which can only be traversed once.  A list can be traversed as many times
as you like, which is quite a few in your code.

>         telstr = ex_phone[0].lower()
>         print telstr
>         if telstr.find(Compare_Name) >= 0:
>             print "\nName found: %s" % Compare_Name
>             namelist[rows][-1] = ex_phone[-1].lower()
>         else:
>             print "Not found %s" % Compare_Name
>             pass
>     return
>
> def name_find():
>     rows = 0
>     for row in names:
>         namelist.append(row)
>         Compare_Name = row[1].lower()
>         finder(Compare_Name,rows)
>         rows = rows+1
>
> if __name__ == '__main__':
>     name_find()
>
> # Writes the list to a file
> wfile  = open('c:/Working/ttest.csv', "wb")
> writer = csv.writer(wfile, delimiter=';')
> for insert in namelist:
>     writer.writerow(insert)
> wfile.close()

As I said before, process both files into lists, one that you treat as
constant (and therefore capitalized) and the other containing the data
you intend to modify.

It'd be much cleaner if you did all that input file parsing stuff in one
function, returning only the lists.  Call it just before calling
name_find().  Similarly, the part you have at the end belongs in a
different function, called just after calling name_find().

There's lots of other stuff that should be cleaner, but you've ignored
nearly all the suggestions from various people.


-- 

DaveA




More information about the Python-list mailing list