Anton Anton - 1 year ago 76
Python Question

Convert a string object with byte characters into a byte object?

I have a string like this:

text = 'b\'"Bill of the one\\xe2\\x80\\x99s store wanted to go outside.\''

That is clearly meant to be byte formatted, however when I look at the object's type, it returns:

<class 'str'>

I tried encoding at byte and then decoding, but this was the result:

'b\'"Bill of the oneĆ¢\x80\x99s store wanted to go outside.\''

How can I get the text properly formatted?

Answer Source

As another possible approach, it seems to me that the string you have is the result of calling repr on a byte object. You can reverse a repr by calling ast.literal_eval:

>>> import ast
>>> x = b'test string'
>>> y = repr(x)
>>> y
"b'test string'"
>>> ast.literal_eval(y)
b'test string'

Or in your case:

>>> x = 'b\'"Bill of the  one\\xe2\\x80\\x99s store wanted to go outside.\''
>>> import ast
>>> ast.literal_eval(x)
b'"Bill of the  one\xe2\x80\x99s store wanted to go outside.'
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download