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


Groups > comp.lang.python > #17938

Re: Backslash Escapes

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python@mrabarnett.plus.com>
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; 'string.': 0.04; 'python': 0.08; 'backslash': 0.09; 'from:addr:python': 0.09; 'parsing': 0.09; 'def': 0.13; 'backslashes': 0.16; 'escapes': 0.16; 'eval': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:name:mrab': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'received:84.92': 0.16; 'received:84.92.122': 0.16; 'received:84.92.122.60': 0.16; 'received:84.93': 0.16; 'received:84.93.230': 0.16; 'reply-to:addr :python-list': 0.16; 'this:': 0.16; 'wrote:': 0.18; 'source.': 0.18; 'file,': 0.19; 'written': 0.20; 'input': 0.22; 'header:In- Reply-To:1': 0.22; 'statement': 0.23; 'dictionary': 0.23; 'guess': 0.26; "i'm": 0.26; 'all,': 0.28; 'unicode': 0.29; "skip:' 10": 0.29; 'skip:p 30': 0.29; 'support,': 0.30; 'header:User-Agent:1': 0.33; 'there': 0.33; 'to:addr:python-list': 0.34; 'received:84': 0.34; 'elegant': 0.34; 'reply-to:addr:python.org': 0.34; 'two': 0.37; 'but': 0.37; 'could': 0.37; 'should': 0.39; 'goes': 0.39; "it's": 0.40; 'to:addr:python.org': 0.40; 'alternative': 0.65; 'safe': 0.70; 'header:Reply-To:1': 0.71; 'reply-to:no real name:2**0': 0.72; 'spaces': 0.73; 'accounts.': 0.77
X-CM-Score 0.00
X-CNFS-Analysis v=2.0 cv=J8QoHXbS c=1 sm=1 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=9jsOeB20M3cA:10 a=OUOv7kDek9cA:10 a=8nJEP1OIZ-IA:10 a=O2pFgWA7kYfueg4p6ssA:9 a=wPNLvfGTeEIA:10 a=0nF1XD0wxitMEM03M9B4ZQ==:117
X-AUTH mrabarnett:2500
Date Mon, 26 Dec 2011 01:26:52 +0000
From MRAB <python@mrabarnett.plus.com>
User-Agent Mozilla/5.0 (Windows NT 5.1; rv:8.0) Gecko/20111105 Thunderbird/8.0
MIME-Version 1.0
To python-list@python.org
Subject Re: Backslash Escapes
References <CAAfDyksFRAmzm1XhQFwSgAD-+ZVxYVuiqMfSMigN3RRkJdFkUw@mail.gmail.com>
In-Reply-To <CAAfDyksFRAmzm1XhQFwSgAD-+ZVxYVuiqMfSMigN3RRkJdFkUw@mail.gmail.com>
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
Reply-To python-list@python.org
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://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 <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.4088.1324862806.27778.python-list@python.org> (permalink)
Lines 22
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1324862806 news.xs4all.nl 6879 [2001:888:2000:d::a6]:34305
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:17938

Show key headers only | View raw


On 26/12/2011 01:04, Felipe O wrote:
> Hi all,
> Whenever I take any input (raw_input, of course!) or I read from a
> file, etc., any backslashes get escaped automatically. Is there any
> elegant way of parsing the backslashes as though they were written in
> a python string. The best I have so far right now goes like this:
>
> def parse_backslash_escapes(input_string):
>      parts = input_string.split("'''")  # That's ' " " " ' without the spaces
>      '"""'.join(eval + p + '"""') for p in parts)
>
> I'm not entirely convinced that it's safe on two accounts.
> + Is that eval statement safe? The input could be coming from an
> unfriendly source.
> + Are there any obscure backslash escapes or other tricks I should be aware of?
>
> I guess the alternative is to make a dictionary of all the escapes I
> want to support, but that sounds tedious and error-prone.
>
Try input_string.decode("string-escape") if input_string is a
bytestring or input_string.decode("unicode-escape") it's a Unicode
string.

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Backslash Escapes MRAB <python@mrabarnett.plus.com> - 2011-12-26 01:26 +0000

csiph-web