I am working on an application which is in Django
. I am trying to store some data structure e.g. dictionary
in MySQL
db. So i use Python Pickle
module. It works fine when i store it in db using pickle.dumps(some_structure)
. My DB field is longblob
mode is binary
.
But when i access the field of model object in django:
obj = someModel.get(pk=1)
some_structure = obj.field
content = pickle.loads(some_structure)
it raises following error:
UnpicklingError: invalid load key, '{'.
Please help me, i have tried google but it does'nt help me, also there is one similar Question but it is not related to my problem as i am storing in db.
UnpicklingError: invalid load key, '<'. This kind of error comes when Weights are complete or some problem with the Weights/ Pickle file because of which UnPickling of weights giving Error. Follow this answer to receive notifications.
You can use the loads() method to unpickle an object that is pickled in the form of a string using the dumps() method, instead of being stored on a disk via the the dump() method. In the following example the car_list object that was pickled to a car_list string is unpickled via the loads() method.
Pickle can be used to serialize Python object structures, which refers to the process of converting an object in the memory to a byte stream that can be stored as a binary file on disk. When we load it back to a Python program, this binary file can be de-serialized back to a Python object.
I encountered this error and I discovered that it was because I had tried to unpickle something that had not originally been pickled.
More specifically, I had stored a Python dict without pickling it, leading to the initial character {
.
Thus, to solve the problem, you should try either:
A corrupt file may cause this issue. Replace the older pickle object with new one. It worked for me.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With