Compare list entry from csv files
Anatoli Hristov
tolidtm at gmail.com
Thu Nov 29 05:22:28 EST 2012
On Tue, Nov 27, 2012 at 9:41 PM, Neil Cerutti <neilc at norwich.edu> wrote:
> On 2012-11-27, Anatoli Hristov <tolidtm at gmail.com> wrote:
>> 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')
>
> csv module expects files to be opened in binary mode in Python
> versions less than version 3.0. For Python versions >= 3.0, you
> use the special keyword argument, newlines='', instead.
>
>> phonelist = []
>> namelist = []
>
> The structure of your program is poor. It's workable for such a
> short script, and sometimes my first cuts are similar, but it's
> better to get out of the habit right away.
>
> Once you get this working the way you'd like you should clean up
> the structure as a service to your future self.
>
>> names = csv.reader(origf, delimiter=';')
>> phones = csv.reader(secfile, delimiter=';')
>
> You csv files don't seem to have header rows, but even so you can
> improve your code by providing fieldnames and using a DictReader
> instead.
>
> name_reader = csv.DictReader(origf, fieldnames=[
> 'Name', 'Blah', 'Phone#'])
>
> Then you can read from records with
>
> name = row['Name']
>
> instead of using bare, undocumented integers.
>
>> for tel in phones:
>> phonelist.append(tel)
>>
>> def finder(name_row,rows):
>> for ex_phone in phonelist:
>> telstr = ex_phone[0].lower()
>> if telstr.find(name_row) >= 0:
>
> This strikes me as a crude way to match names. You don't really
> want Donald to match perfectly with McDonald, do you? Or for
> Smith to match with Smithfield?
>
> Yes, a human being will clean it up, but your program can do a
> better job.
>
>> print "\nName found: %s" % name_row
>> namelist[rows][-1] = ex_phone[-1].lower()
>> else:
>> pass
>> return
>>
>> def name_find():
>> rows = 0
>> for row in names:
>> namelist.append(row)
>> name_row = row[0].lower()
>> finder(name_row,rows)
>> rows = rows+1
>
> You can use the useful enumerate function instead of your own
> counter.
>
> for rows, row in enumerate(names):
>
> ...though I would find 'rownum' or 'num' or just 'i' better than
> the name 'rows', which I find confusing.
>
>> name_find()
>> ofile = open('c:/Working/ttest.csv', "wb")
>> writer = csv.writer(wfile, delimiter=';')
>> for insert in namelist:
>> writer.writerow(insert)
>> wfile.close()
>
> --
> Neil Cerutti
> --
> http://mail.python.org/mailman/listinfo/python-list
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)
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:
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()
More information about the Python-list
mailing list