Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Backslash Escapes

Thread Tools

Backslash Escapes

Felipe O
Posts: n/a
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.


Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
convert ascii escapes into binary form Hans-Peter Jansen Python 3 07-20-2005 08:24 PM
Q: quoting string without escapes Xah Lee Python 2 01-31-2005 09:27 PM
Escapes Sequences Not Working? Rick Brandt XML 13 08-27-2004 11:59 PM
how to replace double backslash with one backslash in string... Vincent Texier Python 3 07-01-2004 01:03 PM
re.sub replacement text \-escapes woe Alexander Schmolck Python 4 02-14-2004 02:30 AM