Dealing with \r in CSV fields in Python2.4

Tim Chase python.list at tim.thechases.com
Wed Sep 4 11:41:17 EDT 2013


On 2013-09-04 16:31, MRAB wrote:
> You could try replacing the '\r' with another character that doesn't
> appear elsewhere and then change it back afterwards.
> 
> MARKER = '\x01'
> 
> def cr_to_marker(f):
>      for line in f:
>          yield line.replace('\r', MARKER)
> 
> def marker_to_cr(item):
>      return item.replace(MARKER, '\r')
> 
> f = file('out.txt', 'rb')
> r = csv.reader(cr_to_marker(f))
> for i, row in enumerate(r): # works in 2.7, fails in 2.4
>      row = [marker_to_cr(item) for item in row]
>      print repr(row)
> f.close()

This works pretty well.  I'm not sure if there's a grave performance
penalty for mucking with strings so much, but at this point my
Care-o-Meter is barely registering, as long as it works.

> Which OS are you using? On Windows the lines (rows) end with
> '\r\n', so the last item of each row will end with '\r', which
> you'll need to strip off. (That would be a problem only if the last
> item of a row could end with '\r'.)

It's on Win32.

-tkc





More information about the Python-list mailing list