Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #17938
| Date | 2011-12-26 01:26 +0000 |
|---|---|
| From | MRAB <python@mrabarnett.plus.com> |
| Subject | Re: Backslash Escapes |
| References | <CAAfDyksFRAmzm1XhQFwSgAD-+ZVxYVuiqMfSMigN3RRkJdFkUw@mail.gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.4088.1324862806.27778.python-list@python.org> (permalink) |
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
Re: Backslash Escapes MRAB <python@mrabarnett.plus.com> - 2011-12-26 01:26 +0000
csiph-web