Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #34065
| Date | 2012-11-29 13:07 +0100 |
|---|---|
| From | Thomas Bach <thbach@students.uni-mainz.de> |
| Subject | Re: Compare list entry from csv files |
| References | (4 earlier) <mailman.313.1354026304.29569.python-list@python.org> <ahk36lFeqmlU3@mid.individual.net> <mailman.320.1354042676.29569.python-list@python.org> <ahkmslFk897U1@mid.individual.net> <CAKhY55OdS7sHDpAg3kdnpqWH5yoORqzE0bTHJVgMWZSHqqZ4kQ@mail.gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.363.1354190916.29569.python-list@python.org> (permalink) |
Can you please cut the message you are responding to the relevant
parts?
On Thu, Nov 29, 2012 at 11:22:28AM +0100, Anatoli Hristov wrote:
> 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.
As I understood it phones is an csv.reader instance and you are
iterating repeatedly over it. But, csv.reader does not work this
way. You either have to reinstantiate phones with a fresh
file-descriptor (not so good) or cache the values in an appropriate
data structure (better) e.g. a list.
> 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')
Note that you never close origf and secfile.
> […]
> # Reads the file with the phone numbers
> # Format "First name","Lastname","Address","City","Country","Phone"
> phones = csv.reader(secfile, delimiter=';')
So this should probably be
PHONES = list(csv.reader(secfile, delimiter=';'))
(in uppercase letters as it is a global)
> […]
> 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()
This should go either in the "if __name__ = …" part or in a function
on its own.
Also have a look at the with statement you can use it in several
places of your code.
There are several other improvements you can make:
+ instead of having the file-names hard coded try to use argparse to
get them from the command-line,
+ let functions stand at their own and use less globals,
+ try to avoid the use of the type of the data structure in the name
(e.g. names is IMHO a better name then namelist),
+ add tests.
Regards,
Thomas
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Re: Compare list entry from csv files Anatoli Hristov <tolidtm@gmail.com> - 2012-11-27 15:24 +0100
Re: Compare list entry from csv files Neil Cerutti <neilc@norwich.edu> - 2012-11-27 15:05 +0000
Re: Compare list entry from csv files Anatoli Hristov <tolidtm@gmail.com> - 2012-11-27 19:57 +0100
Re: Compare list entry from csv files Neil Cerutti <neilc@norwich.edu> - 2012-11-27 20:41 +0000
Re: Compare list entry from csv files Anatoli Hristov <tolidtm@gmail.com> - 2012-11-29 11:22 +0100
Re: Compare list entry from csv files Thomas Bach <thbach@students.uni-mainz.de> - 2012-11-29 13:07 +0100
RE: Compare list entry from csv files "Prasad, Ramit" <ramit.prasad@jpmorgan.com> - 2012-11-29 23:13 +0000
Re: Compare list entry from csv files Dave Angel <d@davea.name> - 2012-11-29 22:17 -0500
Re: Compare list entry from csv files Anatoli Hristov <tolidtm@gmail.com> - 2012-11-30 10:26 +0100
Re: Compare list entry from csv files Anatoli Hristov <tolidtm@gmail.com> - 2012-11-30 10:29 +0100
Re: Compare list entry from csv files Dave Angel <d@davea.name> - 2012-11-27 14:59 -0500
csiph-web