I am trying to convert a Python 2 code into Python 3. I used the pickle.loads
function in Python 2.7, which according to its documentation does the following (https://docs.python.org/2.7/library/pickle.html):
pickle.loads(string)
Read a pickled object hierarchy from a string. Characters in the
string past the pickled object’s representation are ignored.
However its behavior changes in Python 3 (https://docs.python.org/3/library/pickle.html):
pickle.loads(bytes_object, *, fix_imports=True, encoding="ASCII", errors="strict")
Read a pickled object hierarchy from a bytes object and return the
reconstituted object hierarchy specified therein.
In the database, I have a string x
which was the output of pickle.dumps(obj)
performed in Python 2. I want to retrieve obj
in Python 3. When I do pickle.loads(x)
, I receive the following error:
a bytes-like object is required, not 'str'
Changing pickle.loads(x)
into pickle.loads(bytes(x, 'utf-8'))
instead gives the following error:
invalid load key, '\x5c'.
How can I obtain obj
from x
in Python 3?