Backslash Escapes

Discussion in 'Python' started by Felipe O, Dec 26, 2011.

  1. Felipe O

    Felipe O Guest

    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
     
    Felipe O, Dec 26, 2011
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rick Brandt

    Escapes Sequences Not Working?

    Rick Brandt, Aug 25, 2004, in forum: XML
    Replies:
    13
    Views:
    762
    Donald Roby
    Aug 28, 2004
  2. Alexander Schmolck

    re.sub replacement text \-escapes woe

    Alexander Schmolck, Feb 13, 2004, in forum: Python
    Replies:
    4
    Views:
    390
    Alexander Schmolck
    Feb 14, 2004
  3. Vincent Texier
    Replies:
    3
    Views:
    2,466
    Vincent Texier
    Jul 1, 2004
  4. Xah Lee

    Q: quoting string without escapes

    Xah Lee, Jan 31, 2005, in forum: Python
    Replies:
    2
    Views:
    383
    Daniel Bickett
    Jan 31, 2005
  5. Hans-Peter Jansen

    convert ascii escapes into binary form

    Hans-Peter Jansen, Jul 20, 2005, in forum: Python
    Replies:
    3
    Views:
    641
    Hans-Peter Jansen
    Jul 20, 2005
Loading...

Share This Page