In the following code:
>>> import pickle
>>>
>>> class Thing:
def __init__(self):
self.name = "stackoverflow"
self.age = 11
>>> a = pickle.dumps(Thing())
>>> a
b'\x80\x03c__main__\nThing\nq\x00)\x81q\x01}q\x02(X\x04\x00\x00\x00nameq\x03X\r\x00\x00\x00stackoverflowq\x04X\x03\x00\x00\x00ageq\x05K\x0bub.'
>>> str(a)
"b'\\x80\\x03c__main__\\nThing\\nq\\x00)\\x81q\\x01}q\\x02(X\\x04\\x00\\x00\\x00nameq\\x03X\\r\\x00\\x00\\x00stackoverflowq\\x04X\\x03\\x00\\x00\\x00ageq\\x05K\\x0bub.'"
My question is, how do I convert str(a)
back to a
, since once that conversion happens I can easily decode using pickle.loads()
The reason that I am converting to string is that I wish to add str(a)
string to another string that will be further "pickled." Most of the other posts I have seen assume that the bytes are already an encoding of a string or string to byte conversion, none of which this is.