[Tutor] reading Excel CSVs? (was: Re: recommended CSV module?)
Lance E Sloan
lsloan at umich.edu
Thu May 13 11:37:05 EDT 2004
--On Thursday, May 13, 2004 9:16 AM -0400 Lloyd Kvam
<pythonTutor at venix.com> wrote:
> It's hard to keep up with all of the available modules. My experience
> is that the standard csv module is better than the third party
Thanks for the confirmation, Lloyd.
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:
reader = csv.reader(file("true-csv.csv"), dialect='excel')
for row in reader:
Some of the CSV:
PHRASE,ITEM,MONTH,YEAR,PAGE^M"Investments, 2001-2002",Alternative asset
commitment approved with Yorktown Energy Partners V
L.P.,April,2002,283^M"Investments, 2001-2002",Alternative asset
commitment approved with BPG Investment Partnership VI
That CSV came directly from Excel. Note that the "^M" shown here are
actually carriage returns in the file. I just loaded it into vi and copied
a few lines into this message.
When I run the program, I get this error:
Traceback (most recent call last):
File "samplecsv", line 3, in ?
for row in reader:
_csv.Error: newline inside string
I've tried telling the csv.reader that the lineterminator is '\n' or '\r'
in place of its default '\n\r', but neither worked.
How do other folks use the csv module to read Excel files?
BTW, I'm running this on a Mac OS X machine. I've tried this with CSVs
from Excel running on both Windows and Mac and it comes out the same.
(Nice that it's consistent.)
Lance E Sloan, Systems Research Programmer III
U-M WATS: Web Applications, Technologies, and Solutions
Full-service web and database design, development, and hosting.
http://www.itcs.umich.edu/wats/ - "Putting U on the Web"
More information about the Tutor