[Tutor] reading Excel CSVs? (was: Re: recommended CSV
magnus at thinkware.se
Fri May 14 04:21:26 EDT 2004
At 11:37 2004-05-13 -0400, Lance E Sloan wrote:
>I'm trying to use the equivalent of "hello, world" for csv (from the
>example given in the documentation) to read and parse lines from a CSV
>file produced by Excel. The code:
> import csv
> reader = csv.reader(file("true-csv.csv"), dialect='excel')
> for row in reader:
> print row
I think csv is confused by the fact that there is no line feed
character in line endings on the Mac.
Unix uses \n, WinDOS uses \r\n and Mac uses \r.
I'd try opening the files with file("true-csv.csv", "rU")
Note capital U. This is the universal line ending mode. See
file function in Library reference section 2.1.
The manual says you must open in binary mode, i.e.
file("true-csv.csv", "rb"), but I don't think that will
work with Mac file.
I think your Windows files only appear to be like the Mac files
becuase you open them in text mode, so Python converts them to
native line ending format. If you use file("true-csv.csv", "rb")
I think the Windows file (but not the Mac file) will work.
Magnus Lycka (It's really Lyckå), magnus at thinkware.se
Thinkware AB, Sweden, www.thinkware.se
I code Python ~ The Agile Programming Language
More information about the Tutor