unescape escapes in strings
bvdp
bob at mellowood.ca
Mon Feb 23 13:35:35 EST 2009
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???
More information about the Python-list
mailing list