unescape escapes in strings

B

bvdp

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

MRAB

bvdp said:
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"????
foo bar
 
B

bvdp

MRAB said:

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

MRAB

bvdp said:
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???
 
B

bvdp

Perfect ... thanks.

Using "string-escape" does the trick!

Wonderful, this python. And the quick answers on this group.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,059
Latest member
cryptoseoagencies

Latest Threads

Top