Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you decode an ascii string in python?

For example, in your python shell(IDLE):

>>> a = "\x3cdiv\x3e"
>>> print a

The result you get is:

<div>

but if a is an ascii encoded string:

>>> a = "\\x3cdiv\\x3e" ## it's the actual \x3cdiv\x3e string if you read it from a file
>>> print a

The result you get is:

\x3cdiv\x3e

Now what i really want from a is <div>, so I did this:

>>> b = a.decode("ascii")
>>> print b

BUT surprisingly I did NOT get the result I want, it's still:

\x3cdiv\x3e

So basically what do I do to convert a, which is \x3cdiv\x3e to b, which should be <div>?

Thanks

like image 451
Shane Avatar asked May 11 '13 02:05

Shane


2 Answers

>>> a = rb"\x3cdiv\x3e"
>>> a.decode('unicode_escape')
'<div>'

Also check out some interesting codecs.

like image 167
Kabie Avatar answered Oct 27 '22 15:10

Kabie


With python 3.x, you would adapt Kabie answer to

a = b"\x3cdiv\x3e"
a.decode('unicode_escape')

or

a = b"\x3cdiv\x3e"
a.decode('ascii')

both give

>>> a
b'<div>'

What is b prefix for ?

Bytes literals are always prefixed with 'b' or 'B'; they produce an instance of the bytes type instead of the str type. They may only contain ASCII characters; bytes with a numeric value of 128 or greater must be expressed with escapes.

like image 39
kiriloff Avatar answered Oct 27 '22 16:10

kiriloff