Anton Anton - 29 days ago 5
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:

type(text)
<class 'str'>


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

text.encode("utf-8").decode("utf-8")
'b\'"Bill of the oneĆ¢\x80\x99s store wanted to go outside.\''


How can I get the text properly formatted?

Answer

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.'