Safe string escaping?

Discussion in 'Python' started by Grant Olson, Mar 8, 2005.

  1. Grant Olson

    Grant Olson Guest

    I have a data file that has lines like "foo\n\0" where the \n\0 is acutally
    backslash+n+backslash+0. I.E. a repr of the string from python would be
    "foo\\n\\0". I'm trying to convert this string into one that contains
    actual newlines and whatnot. I feel like there has to be a better and safer
    way to do this than eval("'%s'" % "foo\\n\\0") but I'm not finding it.

    Any tips would be appreciated,

    Grant
     
    Grant Olson, Mar 8, 2005
    #1
    1. Advertising

  2. Grant Olson wrote:
    > I have a data file that has lines like "foo\n\0" where the \n\0 is acutally
    > backslash+n+backslash+0. I.E. a repr of the string from python would be
    > "foo\\n\\0". I'm trying to convert this string into one that contains
    > actual newlines and whatnot.


    r"foo\n\0".decode("string_escape")
    --
    Michael Hoffman
     
    Michael Hoffman, Mar 8, 2005
    #2
    1. Advertising

  3. On Mon, 07 Mar 2005 19:24:43 -0500, "Grant Olson" <> wrote:

    >I have a data file that has lines like "foo\n\0" where the \n\0 is acutally
    >backslash+n+backslash+0. I.E. a repr of the string from python would be
    >"foo\\n\\0". I'm trying to convert this string into one that contains
    >actual newlines and whatnot. I feel like there has to be a better and safer
    >way to do this than eval("'%s'" % "foo\\n\\0") but I'm not finding it.
    >
    >Any tips would be appreciated,
    >
    >>> s = "foo\\n\\0"
    >>> s

    'foo\\n\\0'
    >>> list(s)

    ['f', 'o', 'o', '\\', 'n', '\\', '0']
    >>> sd = s.decode('string_escape')
    >>> sd

    'foo\n\x00'
    >>> list(sd)

    ['f', 'o', 'o', '\n', '\x00']

    You could make a de-escaping utility function, e.g.,

    >>> def de_esc(s): return s.decode('string_escape')

    ...
    >>> s

    'foo\\n\\0'
    >>> de_esc(s)

    'foo\n\x00'

    You might want to write a test to verify that it does what you
    expect for the kind of escaped string you are using.

    Regards,
    Bengt Richter
     
    Bengt Richter, Mar 8, 2005
    #3
    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. Gabriel Rossetti
    Replies:
    0
    Views:
    1,330
    Gabriel Rossetti
    Aug 29, 2008
  2. Replies:
    1
    Views:
    343
    Brian Candler
    Aug 12, 2003
  3. Aredridel

    Not just $SAFE, but damn $SAFE

    Aredridel, Sep 2, 2004, in forum: Ruby
    Replies:
    19
    Views:
    244
  4. Farrel Lifson

    $SAFE =4 safe enough?

    Farrel Lifson, Aug 29, 2006, in forum: Ruby
    Replies:
    7
    Views:
    104
    Eric Hodel
    Aug 31, 2006
  5. John Nagle
    Replies:
    5
    Views:
    475
    John Nagle
    Mar 12, 2012
Loading...

Share This Page