Path: csiph.com!eternal-september.org!feeder.eternal-september.org!border1.nntp.ams1.giganews.com!nntp.giganews.com!newsfeed.xs4all.nl!newsfeed7.news.xs4all.nl!nzpost1.xs4all.net!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python,': 0.02; 'sufficient': 0.05; 'tries': 0.05; ':-(': 0.07; 'subject:file': 0.07; 'trailing': 0.07; 'backslash': 0.09; 'ignoring': 0.09; 'oh,': 0.09; 'yeah,': 0.09; 'python': 0.10; 'output': 0.13; 'do,': 0.15; 'wed,': 0.15; 'carriage': 0.16; 'chris,': 0.16; 'comprises': 0.16; 'escapes': 0.16; 'fancy': 0.16; 'naive': 0.16; 'received:74.55.86': 0.16; 'received:74.55.86.74': 0.16; 'received:smtp.webfaction.com': 0.16; 'received:webfaction.com': 0.16; 'soup': 0.16; 'subject:Reading': 0.16; 'wrote:': 0.16; 'skip:l 30': 0.18; 'load': 0.20; '2015': 0.20; 'sep': 0.22; 'trying': 0.22; 'seems': 0.23; 'this:': 0.23; 'tried': 0.24; 'header:In-Reply-To:1': 0.24; "i've": 0.25; 'header:User-Agent:1': 0.26; 'chris': 0.26; 'followed': 0.27; 'handling': 0.27; 'moved': 0.27; '"': 0.29; 'escaped': 0.29; 'pile': 0.29; 'character': 0.29; 'comments': 0.30; 'code': 0.30; 'probably': 0.31; 'source': 0.33; 'file': 0.34; 'handle': 0.34; 'easiest': 0.35; 'mapping': 0.35; 'something': 0.35; 'problem.': 0.35; 'but': 0.36; 'too': 0.36; 'to:addr:python-list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'being': 0.37; 'thanks': 0.37; 'someone': 0.38; 'why': 0.39; 'data': 0.39; 'subject:from': 0.39; 'received:192': 0.39; 'to:addr:python.org': 0.40; 'some': 0.40; 'easy': 0.60; 'your': 0.60; 'australia': 0.61; 'more': 0.63; 'you.': 0.64; 'capable': 0.65; 'else.': 0.66; 'decided': 0.66; 'potentially': 0.67; 'repeat': 0.67; 'header:Reply-To:1': 0.67; 'reply-to:no real name:2**0': 0.71; '(ie': 0.84; 'confusing': 0.84; 'replacements': 0.84; 'western': 0.89; 'hills': 0.93 Reply-To: rhills@medimorphosis.com.au Subject: Re: Reading \n unescaped from a file References: <55E65909.2080507@medimorphosis.com.au> To: python-list@python.org From: Rob Hills X-Enigmail-Draft-Status: N1110 Date: Fri, 4 Sep 2015 00:24:49 +0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.2.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 51 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1441297494 news.xs4all.nl 23739 [2001:888:2000:d::a6]:38286 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:95958 Hi Chris, On 03/09/15 06:10, Chris Angelico wrote: > On Wed, Sep 2, 2015 at 12:03 PM, Rob Hills wrote: >> My mapping file contents look like this: >> >> \r =3D \\n >> =C3=A2=E2=82=AC=C5=93 =3D " > Oh, lovely. Code page 1252 when you're expecting UTF-8. Sadly, you're > likely to have to cope with a whole pile of other mojibake if that > happens :( Yeah, tell me about it!!! > Technically, what's happening is that your "\r" is literally a > backslash followed by the letter r; the transformation of backslash > sequences into single characters is part of Python source code > parsing. (Incidentally, why do you want to change a carriage return > into backslash-n? Seems odd.) > > Probably the easiest solution would be a simple and naive replace(), > looking for some very specific strings and ignoring everything else. > Easy to do, but potentially confusing down the track if someone tries > something fancy :) > > line =3D line.split('#')[:1][0].strip() # trim any trailing comments > line =3D line.replace(r"\r", "\r") # repeat this for as many backslash > escapes as you want to handle > > Be aware that this, while simple, is NOT capable of handling escaped > backslashes. In Python, "\\r" comes out the same as r"\r", but with > this parser, it would come out the same as "\\\r". But it might be > sufficient for you. Thanks for the explanation which has helped me understand the problem.=20 I also tried your approach but wound up with output data that somehow had every single character escaped :-( I've since decided I was being too obsessive trying to load *everything* from my mapping file and have simply hard-coded my two escaped character replacements for now and moved on to more important problems (ie the Windoze Character soup that comprises my data and which I have to clean up!). Thanks again, --=20 Rob Hills Waikiki, Western Australia