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


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

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

Started byTerry Reedy <tjreedy@udel.edu>
First post2013-09-04 17:15 -0400
Last post2013-09-04 17:15 -0400
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 Terry Reedy <tjreedy@udel.edu> - 2013-09-04 17:15 -0400

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

FromTerry Reedy <tjreedy@udel.edu>
Date2013-09-04 17:15 -0400
SubjectRe: Dealing with \r in CSV fields in Python2.4
Message-ID<mailman.63.1378329325.5461.python-list@python.org>
On 9/4/2013 11:04 AM, Tim Chase wrote:
> I've got some old 2.4 code (requires an external lib that hasn't been
> upgraded) that needs to process a CSV file where some of the values
> contain \r characters.  It appears that in more recent versions (just
> tested in 2.7; docs suggest this was changed in 2.5), Python does the
> Right Thing™ and just creates values in the row containing that \r.
> However, in 2.4, the csv module chokes on it with
>
>    _csv.Error: newline inside string
>
> as demoed by the example code at the bottom of this email.

While probably not necessary for this problem, one can use more that one 
Python version to solve a problem. For instance, You could use a current 
version to read the data and transform it so that it can be piped to 2.4 
code running in a subprocess.

>  What's the
> best way to deal with this?  At the moment, I'm just using something
> like
>
>    def unCR(f):
>      for line in f:
>        yield line.replace('\r', '')
>
>    f = file('input.csv', 'rb')
>    for row in csv.reader(unCR(f)):
>      code_to_process(row)
>
> but this throws away data that I'd really prefer to keep if possible.
>
> I know 2.4 isn't exactly popular, and in an ideal world, I'd just
> upgrade to a later 2.x version that does what I need.  Any old-time
> 2.4 pythonistas have sage advice for me?
>
> -tkc
>
>
> from cStringIO import StringIO
> import csv
> f = file('out.txt', 'wb')
> w = csv.writer(f)
> w.writerow(["One", "Two"])
> w.writerow(["First\rSecond", "Third"])
> f.close()
>
> f = file('out.txt', 'rb')
> r = csv.reader(f)
> for i, row in enumerate(r): # works in 2.7, fails in 2.4
>      print repr(row)
> f.close()
>
>


-- 
Terry Jan Reedy

[toc] | [standalone]


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


csiph-web