unescape escapes in strings

bvdp bob at mellowood.ca
Mon Feb 23 19:05:55 CET 2009

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"????

More information about the Python-list mailing list