Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #53633 > unrolled thread

Re: Dealing with \r in CSV fields in Python2.4

Started byTim Chase <python.list@tim.thechases.com>
First post2013-09-04 10:41 -0500
Last post2013-09-04 10:41 -0500
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Dealing with \r in CSV fields in Python2.4 Tim Chase <python.list@tim.thechases.com> - 2013-09-04 10:41 -0500

#53633 — Re: Dealing with \r in CSV fields in Python2.4

FromTim Chase <python.list@tim.thechases.com>
Date2013-09-04 10:41 -0500
SubjectRe: Dealing with \r in CSV fields in Python2.4
Message-ID<mailman.50.1378309209.5461.python-list@python.org>
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

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web