Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #95959
| Path | csiph.com!goblin3!goblin1!goblin.stu.neva.ru!uio.no!news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!newsgate.news.xs4all.nl!nzpost1.xs4all.net!not-for-mail |
|---|---|
| Return-Path | <rhills@medimorphosis.com.au> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.002 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; '(python': 0.05; 'mrab': 0.05; '"as': 0.07; 'bits': 0.07; 'subject:file': 0.07; 'literal': 0.09; 'python': 0.10; 'output': 0.13; 'file;': 0.16; 'first:': 0.16; 'googling': 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; 'subject:Reading': 0.16; 'suggestion.': 0.16; 'wrote:': 0.16; 'later': 0.16; 'string': 0.17; 'debugging': 0.18; 'transform': 0.18; 'load': 0.20; 'cheers,': 0.22; 'file.': 0.22; 'trying': 0.22; 'code,': 0.23; 'seems': 0.23; 'this:': 0.23; "haven't": 0.24; 'second': 0.24; 'header:In-Reply-To:1': 0.24; "i've": 0.25; 'header:User-Agent:1': 0.26; 'figure': 0.27; 'format,': 0.27; 'moved': 0.27; 'idea': 0.28; 'developing': 0.28; 'another.': 0.29; 'coded': 0.29; 'escaped': 0.29; 'figured': 0.29; 'issues.': 0.29; 'character': 0.29; 'convert': 0.29; 'code': 0.30; "can't": 0.32; 'problem': 0.33; 'though.': 0.33; "i'll": 0.33; 'changing': 0.34; 'file': 0.34; 'skip:d 20': 0.34; 'text': 0.35; 'mapping': 0.35; 'something': 0.35; 'but': 0.36; 'too': 0.36; 'to:addr:python- list': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'being': 0.37; 'thanks': 0.37; 'desired': 0.37; 'represent': 0.38; 'hi,': 0.38; 'why': 0.39; 'data': 0.39; 'format': 0.39; 'subject:from': 0.39; 'received:192': 0.39; 'to:addr:python.org': 0.40; 'australia': 0.61; 'different': 0.63; 'decided': 0.66; 'header:Reply-To:1': 0.67; 'incoming': 0.70; 'reply-to:no real name:2**0': 0.71; 'replacements': 0.84; 'western': 0.89; 'either:': 0.91; 'hills': 0.93 |
| Reply-To | rhills@medimorphosis.com.au |
| Subject | Re: Reading \n unescaped from a file |
| References | <55E65909.2080507@medimorphosis.com.au> <55E778C7.7050802@mrabarnett.plus.com> |
| To | python-list@python.org |
| From | Rob Hills <rhills@medimorphosis.com.au> |
| X-Enigmail-Draft-Status | N1110 |
| Date | Fri, 4 Sep 2015 00:32:30 +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 | <55E778C7.7050802@mrabarnett.plus.com> |
| 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 <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.88.1441298022.8327.python-list@python.org> (permalink) |
| Lines | 70 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1441298022 news.xs4all.nl 23836 [2001:888:2000:d::a6]:50646 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:95959 |
Show key headers only | View raw
Hi,
On 03/09/15 06:31, MRAB wrote:
> On 2015-09-02 03:03, Rob Hills wrote:
>> I am developing code (Python 3.4) that transforms text data from one
>> format to another.
>>
>> As part of the process, I had a set of hard-coded str.replace(...)
>> functions that I used to clean up the incoming text into the desired
>> output format, something like this:
>>
>> dataIn = dataIn.replace('\r', '\\n') # Tidy up linefeeds
>> dataIn = dataIn.replace('<','<') # Tidy up < character
>> dataIn = dataIn.replace('>','>') # Tidy up < character
>> dataIn = dataIn.replace('o','o') # No idea why but lots of
>> these: convert to 'o' character
>> dataIn = dataIn.replace('f','f') # .. and these: convert to
>> 'f' character
>> dataIn = dataIn.replace('e','e') # .. 'e'
>> dataIn = dataIn.replace('O','O') # .. 'O'
>>
> The problem with this approach is that the order of the replacements
> matters. For example, changing '<' to '<' and then '&' to '&'
> can give a different result to changing '&' to '&' and then '<'
> to '<'. If you started with the string '&lt;', then the first order
> would go '&lt;' => '&lt;' => '<', whereas the second order
> would go '&lt;' => '<' => '<'.
Ah yes, thanks for reminding me about that. I've since modified my code
to use a collections.OrderedDict to store my mappings.
...
>> This all works "as advertised" */except/* for the '\r' => '\\n'
>> replacement. Debugging the code, I see that my '\r' character is
>> "escaped" to '\\r' and the '\\n' to '\\\\n' when they are read in from
>> the file.
>>
>> I've been googling hard and reading the Python docs, trying to get my
>> head around character encoding, but I just can't figure out how to get
>> these bits of code to do what I want.
>>
>> It seems to me that I need to either:
>>
>> * change the way I represent '\r' and '\\n' in my mapping file; or
>> * transform them somehow when I read them in
>>
>> However, I haven't figured out how to do either of these.
>>
> Try ast.literal_eval, although you'd need to make it look like a string
> literal first:
Thanks for the suggestion. For now, I've decided I was being too
pedantic trying to load my two escaped strings from a file and I've
simply hard coded them and moved on to other issues. I'll try this idea
later on though.
Cheers,
--
Rob Hills
Waikiki, Western Australia
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Reading \n unescaped from a file Rob Hills <rhills@medimorphosis.com.au> - 2015-09-04 00:32 +0800
csiph-web