Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #53653
| Path | csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <python-python-list@m.gmane.org> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.000 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'transform': 0.07; 'string': 0.09; "'')": 0.09; 'f.close()': 0.09; 'newline': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:fields': 0.09; 'throws': 0.09; 'python': 0.11; 'def': 0.12; 'jan': 0.12; 'suggest': 0.14; 'creates': 0.14; "'rb')": 0.16; '(just': 0.16; '-tkc': 0.16; 'cstringio': 0.16; 'csv': 0.16; 'pythonistas': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'stringio': 0.16; 'subject:CSV': 0.16; 'wrote:': 0.18; 'module': 0.19; "hasn't": 0.19; 'later': 0.20; 'appears': 0.22; 'example': 0.22; 'import': 0.22; 'print': 0.22; 'this?': 0.23; 'header:User-Agent:1': 0.23; '2.x': 0.24; 'instance,': 0.24; 'versions': 0.24; "i've": 0.25; 'world,': 0.26; 'values': 0.27; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'external': 0.29; 'am,': 0.29; 'ideal': 0.29; 'tim': 0.29; "i'm": 0.30; 'code': 0.31; 'chase': 0.31; 'file': 0.32; 'probably': 0.32; 'me?': 0.32; 'running': 0.33; "i'd": 0.34; 'could': 0.34; 'subject:with': 0.35; 'advice': 0.35; 'possible.': 0.35; 'problem.': 0.35; 'something': 0.35; 'but': 0.35; 'version': 0.36; 'really': 0.36; 'yield': 0.36; 'to:addr:python-list': 0.38; 'recent': 0.39; 'does': 0.39; 'to:addr:python.org': 0.39; 'changed': 0.39; 'received:org': 0.40; 'read': 0.60; 'solve': 0.60; 'received:173': 0.61; 'more': 0.64; 'bottom': 0.67; 'containing': 0.69; '2.5),': 0.84; 'received:fios.verizon.net': 0.84 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| To | python-list@python.org |
| From | Terry Reedy <tjreedy@udel.edu> |
| Subject | Re: Dealing with \r in CSV fields in Python2.4 |
| Date | Wed, 04 Sep 2013 17:15:10 -0400 |
| References | <20130904100403.163b42bd@bigbox.christie.dr> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=UTF-8; format=flowed |
| Content-Transfer-Encoding | quoted-printable |
| X-Gmane-NNTP-Posting-Host | pool-173-75-251-66.phlapa.fios.verizon.net |
| User-Agent | Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/20130801 Thunderbird/17.0.8 |
| In-Reply-To | <20130904100403.163b42bd@bigbox.christie.dr> |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.63.1378329325.5461.python-list@python.org> (permalink) |
| Lines | 63 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1378329325 news.xs4all.nl 15999 [2001:888:2000:d::a6]:42547 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:53653 |
Show key headers only | View raw
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
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Dealing with \r in CSV fields in Python2.4 Terry Reedy <tjreedy@udel.edu> - 2013-09-04 17:15 -0400
csiph-web