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

Tim Chase python.list at tim.thechases.com
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 testr.py
   from cStringIO import StringIO
   from csv import DictReader

   data = StringIO(
     'one,"two two",three\n'
     '"1a\r1b","2a\n2b","3a\r\n3b"\n'
     '"1x\r1y","2x\n2y","3x\r\n3y"\n'
     )

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


tim at rubbish:~/tmp$ python2.5 testr.py
'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 testr.py
Traceback (most recent call last):
   File "testr.py", line 12, in ?
     for row in dr:
   File "/usr/lib/python2.4/csv.py", line 109, in next
     row = self.reader.next()
_csv.Error: newline inside string



-tkc






More information about the Python-list mailing list