Using csv.DictReader with \r\n in the middle of fields

Tim Chase python.list at
Thu Oct 14 03:03:30 CEST 2010

On 10/13/10 11:33, Dennis Lee Bieber wrote:
> 	While I've not tested it, my understanding of the documentation
> indicates that the reader /can/ handle multi-line fields IF QUOTED...
> (you may still have to strip the terminator out of the description data
> after it has been loaded).
> 	That is:
> Some Title~Subtitle~Episode~"A description with<cr><lf>
> an embedded new line terminator"~Date
> should be properly parsed.

I believe this was fixed in 2.5  The following worked in 2.5 but 
2.4 rejected it:

   # saved as
   from cStringIO import StringIO
   from csv import DictReader

   data = StringIO(
     'one,"two two",three\n'

   dr = DictReader(data)
   for row in dr:
     for k,v in row.iteritems():
       print '%r ==> %r' % (k,v)

tim at rubbish:~/tmp$ python2.5
'two two' ==> '2a\n2b'
'three' ==> '3a\r\n3b'
'one' ==> '1a\r1b'
'two two' ==> '2x\n2y'
'three' ==> '3x\r\n3y'
'one' ==> '1x\r1y'
tim at rubbish:~/tmp$ python2.4
Traceback (most recent call last):
   File "", line 12, in ?
     for row in dr:
   File "/usr/lib/python2.4/", line 109, in next
     row =
_csv.Error: newline inside string


More information about the Python-list mailing list