SimplePrograms challenge
John Machin
sjmachin at lexicon.net
Mon Jun 11 19:20:27 EDT 2007
On Jun 12, 8:51 am, infidel <saint.infi... at gmail.com> wrote:
> # reading CSV files, tuple-unpacking
> import csv
>
> #pacific.csv contains:
> #1,CA,California
> #2,AK,Alaska
> #3,OR,Oregon
> #4,WA,Washington
> #5,HI,Hawaii
>
> reader = csv.reader(open('pacific.csv'))
For generality and portability, this should be:
reader = csv.reader(open('pacific.csv', 'rb'))
> for id, abbr, name in reader:
> print '%s is abbreviated: "%s"' % (name, abbr)
and this example doesn't demonstrate why one should use the csv module
instead of:
for line in open('pacific.csv'):
id, abbr, name = line.rstrip().split(',')
# etc
which is quite adequate for the simplistic example file.
More information about the Python-list
mailing list