[Csv] csv module generating an invalid line?

sjmachin at lexicon.net sjmachin at lexicon.net
Fri Jan 21 02:17:47 CET 2005


On 20 Jan 2005 at 15:04, Skip Montanaro wrote:

> 
> We use the csv module in the SpamBayes project as an interchange
> format (*). It's generating, in part, a file like this:
> 
>     ...
>     simplymaya,0,1
>     entitled,1,1
>     "subject:          
> ",0,1
>     depression.,1,0
>     ...
> 
> Note the CR inside the quoted field (third line).  When I try to read
> that file, blammo!  This example consists of a junk.csv file with just
> the above four lines:
> 
>     >>> for row in csv.reader(open("junk.csv")):
>     ...   print row
>     ... 
>     ['simplymaya', '0', '1']
>     ['entitled', '1', '1']
>     Traceback (most recent call last):
>       File "<stdin>", line 1, in ?
>     Error: newline inside string
> 
> I think one way or the other the csv module is broken.  Either it
> should be able to read this csv file or it should somehow generate it
> differently.
> 
> I've confirmed this with Python from CVS (as of Jan 5 05), the 2.4
> maintenance branch (as of Dec 26 04) and Python 2.3.4.
> 
> Thoughts?
> 
> Skip


>>> file('junk.csv', 'rb').read()
'simplymaya,0,1\r\nentitled,1,1\r\n"subject:          \r",0,1\r\ndepression.,1,0\r\n'

Your junk.csv appears to be a valid csv file. The field containing the embedded \r is 
quoted properly. 

It's the _reader_ that's broken. Doubly so: (1) chucking an exception (2) calling \r a 
"newline".

As you say, it's broken in 2.3 as well:

Python 2.4 (#60, Nov 30 2004, 11:49:19) [MSC v.1310 32 bit (Intel)] on win32
>>> import csv
>>> r = csv.reader(file('junk.csv','rb'))
>>> contents = list(r)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
_csv.Error: newline inside string
>>>

Python 2.3.4 (#53, May 25 2004, 21:17:02) [MSC v.1200 32 bit (Intel)] on win32
>>> import csv
>>> list(csv.reader(file('junk.csv', 'rb')))
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
_csv.Error: newline inside string
>>>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/csv/attachments/20050121/f0e4199a/attachment.html 


More information about the Csv mailing list