unescape escapes in strings

Discussion in 'Python' started by bvdp, Feb 23, 2009.

  1. bvdp

    bvdp Guest

    When reading lines of data from a file in the from (no quotes!)

    foo\x20bar

    and I assign to a variable in a line line like:

    f = file('infile', 'r')
    for a in f:
    print a

    the string is read in as string with the literal characters 'f', 'o' ...
    'x' , '2' ...

    as compared to an assignment like:

    a="foo\x20bar"

    which is identical to

    a="foo bar"

    Okay, so far ... I think this is what I want since my program is using
    space characters as delimiters and I'm trying to use the \x20 notation
    to avoid splitting.

    But, now the problem. When I finally assign the string with the \x20 to
    a variable the literals are still there. And here I really want them all
    nicely converted to the desired values.

    So, the question is: is there an "unescape()" for strings so that
    "foo\x20bar" is converted to "foo bar"????
    bvdp, Feb 23, 2009
    #1
    1. Advertising

  2. bvdp

    MRAB Guest

    bvdp wrote:
    >
    > When reading lines of data from a file in the from (no quotes!)
    >
    > foo\x20bar
    >
    > and I assign to a variable in a line line like:
    >
    > f = file('infile', 'r')
    > for a in f:
    > print a
    >
    > the string is read in as string with the literal characters 'f', 'o' ...
    > 'x' , '2' ...
    >
    > as compared to an assignment like:
    >
    > a="foo\x20bar"
    >
    > which is identical to
    >
    > a="foo bar"
    >
    > Okay, so far ... I think this is what I want since my program is using
    > space characters as delimiters and I'm trying to use the \x20 notation
    > to avoid splitting.
    >
    > But, now the problem. When I finally assign the string with the \x20 to
    > a variable the literals are still there. And here I really want them all
    > nicely converted to the desired values.
    >
    > So, the question is: is there an "unescape()" for strings so that
    > "foo\x20bar" is converted to "foo bar"????
    >
    >>> a = r"foo\x20bar"
    >>> print a

    foo\x20bar
    >>> a = a.decode("string-escape")
    >>> print a

    foo bar
    MRAB, Feb 23, 2009
    #2
    1. Advertising

  3. bvdp

    bvdp Guest

    MRAB wrote:
    > bvdp wrote:
    >>
    >> When reading lines of data from a file in the from (no quotes!)
    >>
    >> foo\x20bar
    >>
    >> and I assign to a variable in a line line like:
    >>
    >> f = file('infile', 'r')
    >> for a in f:
    >> print a
    >>
    >> the string is read in as string with the literal characters 'f', 'o'
    >> ... 'x' , '2' ...
    >>
    >> as compared to an assignment like:
    >>
    >> a="foo\x20bar"
    >>
    >> which is identical to
    >>
    >> a="foo bar"
    >>
    >> Okay, so far ... I think this is what I want since my program is using
    >> space characters as delimiters and I'm trying to use the \x20 notation
    >> to avoid splitting.
    >>
    >> But, now the problem. When I finally assign the string with the \x20
    >> to a variable the literals are still there. And here I really want
    >> them all nicely converted to the desired values.
    >>
    >> So, the question is: is there an "unescape()" for strings so that
    >> "foo\x20bar" is converted to "foo bar"????
    >>
    > >>> a = r"foo\x20bar"
    > >>> print a

    > foo\x20bar
    > >>> a = a.decode("string-escape")
    > >>> print a

    > foo bar
    >


    Thanks ... I think in my original testing I tried decode() but it didn't
    work. Testing more ...

    the file has 2 lines:
    foo bar
    foo\x20bar

    and the program to read is:
    f=file('in', 'r')
    for a in f:
    a = a.strip()
    a=a.decode()
    print list(a)

    I get:

    python read.py
    []
    [u'f', u'o', u'o', u' ', u'b', u'a', u'r']
    [u'f', u'o', u'o', u'\\', u'x', u'2', u'0', u'b', u'a', u'r']

    So, the \x20 is still literal.

    Any other ideas??? I suppose I could write a re expression ... but
    surely that is not needed???
    bvdp, Feb 23, 2009
    #3
  4. bvdp

    MRAB Guest

    bvdp wrote:
    > MRAB wrote:
    >> bvdp wrote:
    >>>
    >>> When reading lines of data from a file in the from (no quotes!)
    >>>
    >>> foo\x20bar
    >>>
    >>> and I assign to a variable in a line line like:
    >>>
    >>> f = file('infile', 'r')
    >>> for a in f:
    >>> print a
    >>>
    >>> the string is read in as string with the literal characters 'f', 'o'
    >>> ... 'x' , '2' ...
    >>>
    >>> as compared to an assignment like:
    >>>
    >>> a="foo\x20bar"
    >>>
    >>> which is identical to
    >>>
    >>> a="foo bar"
    >>>
    >>> Okay, so far ... I think this is what I want since my program is
    >>> using space characters as delimiters and I'm trying to use the \x20
    >>> notation to avoid splitting.
    >>>
    >>> But, now the problem. When I finally assign the string with the \x20
    >>> to a variable the literals are still there. And here I really want
    >>> them all nicely converted to the desired values.
    >>>
    >>> So, the question is: is there an "unescape()" for strings so that
    >>> "foo\x20bar" is converted to "foo bar"????
    >>>
    >> >>> a = r"foo\x20bar"
    >> >>> print a

    >> foo\x20bar
    >> >>> a = a.decode("string-escape")
    >> >>> print a

    >> foo bar
    >>

    >
    > Thanks ... I think in my original testing I tried decode() but it didn't
    > work. Testing more ...
    >
    > the file has 2 lines:
    > foo bar
    > foo\x20bar
    >
    > and the program to read is:
    > f=file('in', 'r')
    > for a in f:
    > a = a.strip()
    > a=a.decode()


    You didn't specify what kind of decoding you want!

    > print list(a)
    >
    > I get:
    >
    > python read.py
    > []
    > [u'f', u'o', u'o', u' ', u'b', u'a', u'r']
    > [u'f', u'o', u'o', u'\\', u'x', u'2', u'0', u'b', u'a', u'r']
    >
    > So, the \x20 is still literal.
    >
    > Any other ideas??? I suppose I could write a re expression ... but
    > surely that is not needed???
    >
    MRAB, Feb 23, 2009
    #4
  5. bvdp

    bvdp Guest

    Perfect ... thanks.

    >>> >>> a = a.decode("string-escape")


    Using "string-escape" does the trick!

    Wonderful, this python. And the quick answers on this group.
    bvdp, Feb 23, 2009
    #5
    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:
    733
    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:
    372
    Alexander Schmolck
    Feb 14, 2004
  3. Xah Lee

    Q: quoting string without escapes

    Xah Lee, Jan 31, 2005, in forum: Python
    Replies:
    2
    Views:
    362
    Daniel Bickett
    Jan 31, 2005
  4. Tony Augustine
    Replies:
    0
    Views:
    350
    Tony Augustine
    Jul 22, 2010
  5. Replies:
    10
    Views:
    711
    Jason Friedman
    Jun 16, 2012
Loading...

Share This Page