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


Groups > comp.lang.python > #53633

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

Path csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python.list@tim.thechases.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.016
X-Spam-Evidence '*H*': 0.97; '*S*': 0.00; 'mrab': 0.05; 'f.close()': 0.09; 'subject:fields': 0.09; 'works.': 0.09; 'def': 0.12; 'windows': 0.15; "'rb')": 0.16; '-tkc': 0.16; 'afterwards.': 0.16; 'from:addr:python.list': 0.16; 'from:addr:tim.thechases.com': 0.16; 'from:name:tim chase': 0.16; 'marker': 0.16; 'subject:CSV': 0.16; 'wrote:': 0.18; 'replacing': 0.19; 'print': 0.22; 'header :In-Reply-To:1': 0.27; 'point': 0.28; 'appear': 0.29; 'character': 0.29; "doesn't": 0.30; "i'm": 0.30; 'lines': 0.31; 'strip': 0.31; 'another': 0.32; 'could': 0.34; 'problem': 0.35; 'subject:with': 0.35; 'but': 0.35; 'yield': 0.36; 'charset:us-ascii': 0.36; 'performance': 0.37; 'skip:[ 10': 0.38; 'to:addr:python-list': 0.38; 'sure': 0.39; 'to:addr:python.org': 0.39; 'back': 0.62; "you'll": 0.62; '(that': 0.65; 'grave': 0.84; 'much,': 0.84; 'penalty': 0.84; 'received:50.22': 0.84; 'registering,': 0.84
Date Wed, 4 Sep 2013 10:41:17 -0500
From Tim Chase <python.list@tim.thechases.com>
To python-list@python.org
Subject Re: Dealing with \r in CSV fields in Python2.4
In-Reply-To <5227523A.7040900@mrabarnett.plus.com>
References <20130904100403.163b42bd@bigbox.christie.dr> <5227523A.7040900@mrabarnett.plus.com>
X-Mailer Claws Mail 3.8.1 (GTK+ 2.24.10; x86_64-pc-linux-gnu)
Mime-Version 1.0
Content-Type text/plain; charset=US-ASCII
Content-Transfer-Encoding 7bit
X-AntiAbuse This header was added to track abuse, please include it with any abuse report
X-AntiAbuse Primary Hostname - boston.accountservergroup.com
X-AntiAbuse Original Domain - python.org
X-AntiAbuse Originator/Caller UID/GID - [47 12] / [47 12]
X-AntiAbuse Sender Address Domain - tim.thechases.com
X-Get-Message-Sender-Via boston.accountservergroup.com: none
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.50.1378309209.5461.python-list@python.org> (permalink)
Lines 34
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1378309209 news.xs4all.nl 15971 [2001:888:2000:d::a6]:46562
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:53633

Show key headers only | View raw


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


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