Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #17939 > unrolled thread
| Started by | Devin Jeanpierre <jeanpierreda@gmail.com> |
|---|---|
| First post | 2011-12-25 20:26 -0500 |
| Last post | 2011-12-25 20:26 -0500 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: Backslash Escapes Devin Jeanpierre <jeanpierreda@gmail.com> - 2011-12-25 20:26 -0500
| From | Devin Jeanpierre <jeanpierreda@gmail.com> |
|---|---|
| Date | 2011-12-25 20:26 -0500 |
| Subject | Re: Backslash Escapes |
| Message-ID | <mailman.4089.1324862843.27778.python-list@python.org> |
> 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 top | Article view | comp.lang.python
csiph-web