Advertisements
I have a string from a database request as follows:
result = "b'\x00\x00\x00\x00\x07\x80\x00\x03'"
type(result) -> <class 'str'>
How do I convert it to bytes? It should be like this:
a = b'\x00\x00\x00\x00\x07\x80\x00\x03'
type(a) -> <class 'bytes'>
>Solution :
From what I understood (I have a terrible understanding of english):
You can use the eval()
or the exec()
function to convert a string to any Python object you want.
Here’s an example:
a = "b'\x00\x00\x00\x00\x07\x80\x00\x03'"
x = eval(a.encode("unicode-escape").decode())
print(type(x)) -> bytes
Same thing for the exec
function.