unescape escapes in strings
bob at mellowood.ca
Mon Feb 23 19:35:35 CET 2009
> bvdp wrote:
>> When reading lines of data from a file in the from (no quotes!)
>> 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:
>> 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
> >>> 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:
and the program to read is:
for a in f:
a = a.strip()
[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???
More information about the Python-list