Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #53633
| Date | 2013-09-04 10:41 -0500 |
|---|---|
| From | Tim Chase <python.list@tim.thechases.com> |
| Subject | Re: Dealing with \r in CSV fields in Python2.4 |
| References | <20130904100403.163b42bd@bigbox.christie.dr> <5227523A.7040900@mrabarnett.plus.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.50.1378309209.5461.python-list@python.org> (permalink) |
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
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Dealing with \r in CSV fields in Python2.4 Tim Chase <python.list@tim.thechases.com> - 2013-09-04 10:41 -0500
csiph-web