parse a csv file into a text file

Roy Smith roy at panix.com
Wed Feb 5 19:33:00 EST 2014


In article <5c268845-003f-4e24-b27a-c89e9fbfcc6c at googlegroups.com>,
 Zhen Zhang <zhen.zhang.uoft at gmail.com> wrote:

> [code]
> 
> import csv
> file = open('raw.csv')
> reader = csv.reader(file)
> 
> f = open('NicelyDone.text','w')
> 
> for line in reader:
>       f.write("%s %s"%line[1],%line[5])
> 
> [/code]

Are you using Python 2 or 3?

> Here is my question:
> 1:What is the data format for line[1],

That's something you can easily figure out by printing out the 
intermediate values.  Try something like:

> for line in reader:
>       print type(line[1]), repr(line(1))

See if that prints what you expect.

> how come f.write() does not work.

What does "does not work" mean?  What does get written to the file?  Or 
do you get some sort of error?

I'm pretty sure I see your error, but I'm trying to lead you to being 
able to diagnose it yourself :-)



More information about the Python-list mailing list