Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UnpicklingError invalid load key, '{'

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.

like image 985
Aamir Rind Avatar asked Nov 13 '11 10:11

Aamir Rind


People also ask

What is UnPickling error in Python?

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.

How do I Unpickle a pickle file?

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.

What is .pickle file?

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.


2 Answers

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:

  • Not unpickling the string upon retrieval from the datastore, or
  • Verifying that objects are being pickled prior to insertion into the datastore
like image 179
einnocent Avatar answered Sep 28 '22 09:09

einnocent


A corrupt file may cause this issue. Replace the older pickle object with new one. It worked for me.

like image 33
Sunil Sharma Avatar answered Sep 28 '22 11:09

Sunil Sharma