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


Groups > comp.lang.python > #17939

Re: Backslash Escapes

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!newsfeed.eweka.nl!eweka.nl!feeder3.eweka.nl!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <jeanpierreda@gmail.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; 'python,': 0.01; 'string.': 0.04; 'python': 0.08; 'backslash': 0.09; 'parsing': 0.09; '25,': 0.12; 'def': 0.13; 'backslashes': 0.16; 'escapes': 0.16; 'eval': 0.16; 'cc:addr:python-list': 0.16; 'this:': 0.16; 'wrote:': 0.18; 'thanks,': 0.18; '>>>': 0.18; 'bytes': 0.18; 'source.': 0.18; 'file,': 0.19; 'cc:no real name:2**0': 0.20; 'written': 0.20; 'input': 0.22; 'dec': 0.22; 'header:In-Reply-To:1': 0.22; 'statement': 0.23; 'dictionary': 0.23; 'cc:2**0': 0.24; 'guess': 0.26; "i'm": 0.26; 'all,': 0.28; 'url:mailman': 0.28; 'message- id:@mail.gmail.com': 0.28; "skip:' 10": 0.29; 'print': 0.29; 'cc:addr:python.org': 0.29; 'pm,': 0.29; 'skip:p 30': 0.29; 'support,': 0.30; 'received:209.85.210.46': 0.30; 'received:mail- pz0-f46.google.com': 0.30; 'sun,': 0.30; 'url:listinfo': 0.32; 'there': 0.33; 'elegant': 0.34; 'url:python': 0.36; 'are.': 0.37; 'two': 0.37; 'but': 0.37; 'received:google.com': 0.37; 'could': 0.37; 'received:209.85': 0.38; 'url:org': 0.39; 'should': 0.39; 'goes': 0.39; "it's": 0.40; 'received:209': 0.40; '2011': 0.61; 'alternative': 0.65; 'safe': 0.70; 'spaces': 0.73; 'skip:\xc2 10': 0.74; 'accounts.': 0.77
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type:content-transfer-encoding; bh=jYW3p4WUw9a5LoPVR3FJcxEO2jErzGG40wP9ifG2Emo=; b=pXltIKk8mv3srx2R2+J/W+k54E/8qd5CY1DzJtq2YS+fWcmgTl5EOYLjNVlSz2sqXm lndFYWicZOHv/nvx94ED6/IxjZoLVpWTqv3B8nAKbO7zidKwsLvBQRE8R06W7FgcxCyZ ju+Tra1tgeH4BzwQXPuTNmXESELs0lDetFbE8=
MIME-Version 1.0
In-Reply-To <CAAfDyksFRAmzm1XhQFwSgAD-+ZVxYVuiqMfSMigN3RRkJdFkUw@mail.gmail.com>
References <CAAfDyksFRAmzm1XhQFwSgAD-+ZVxYVuiqMfSMigN3RRkJdFkUw@mail.gmail.com>
From Devin Jeanpierre <jeanpierreda@gmail.com>
Date Sun, 25 Dec 2011 20:26:39 -0500
Subject Re: Backslash Escapes
To Felipe O <pip.261@gmail.com>
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding quoted-printable
Cc python-list@python.org
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
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.4089.1324862843.27778.python-list@python.org> (permalink)
Lines 43
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1324862843 news.xs4all.nl 6894 [2001:888:2000:d::a6]:34719
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:17939

Show key headers only | View raw


> Whenever I take any input (raw_input, of course!) or I read from a
> file, etc., any backslashes get escaped automatically.

They don't get escaped, they get... treated as the bytes that they
are. If you want them to mean what they do in Python, use the
'string_escape' codec.

    >>> x = r'nyan\nyan'
    >>> print x
    nyan\nyan
    >>> print x.decode('string_escape')
    nyan
    yan

-- Devin

On Sun, Dec 25, 2011 at 8:04 PM, Felipe O <pip.261@gmail.com> 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.
>
> Thanks,
>
> Felipe
> --
> http://mail.python.org/mailman/listinfo/python-list

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


Thread

Re: Backslash Escapes Devin Jeanpierre <jeanpierreda@gmail.com> - 2011-12-25 20:26 -0500

csiph-web