Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python pickle/unpickle a list to/from a file

Tags:

I have a list that looks like this:

a = [['a string', [0, 0, 0], [22, 'bee sting']], ['see string',      [0, 2, 0], [22, 'd string']]] 

and am having problems saving it and retrieving it.

I can save it ok using pickle:

with open('afile','w') as f:     pickle.dump(a,f) 

but get the following error when I try to load it:

pickle.load('afile')  Traceback (most recent call last):   File "<pyshell#116>", line 1, in <module>     pickle.load('afile')   File "C:\Python27\lib\pickle.py", line 1378, in load     return Unpickler(file).load()   File "C:\Python27\lib\pickle.py", line 841, in __init__     self.readline = file.readline AttributeError: 'str' object has no attribute 'readline' 

I had thought that I could convert to a numpy array and use save, savez or savetxt. However I get the following error:

>>> np.array([a])  Traceback (most recent call last):   File "<pyshell#122>", line 1, in <module>     np.array([a]) ValueError: cannot set an array element with a sequence 
like image 296
atomh33ls Avatar asked Aug 14 '13 10:08

atomh33ls


People also ask

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.

Can you pickle a list in Python?

You can also use pickle to retrieve your original list, loading from the saved file. So, first build a list, then use pickle. dump to send it to a file... Python 3.4.

How do you deserialize a pickle in Python?

The pickle moduleDeserialization is the process of converting a byte stream to Python object. This process is also called pickling/unpickling or marshalling/unmarshalling. The pickletools module contains tools for analyzing data streams generated by pickle. Note: data serialization with the pickle module is insecure.

What is the difference between pickling and Unpickling?

“Pickling” is the process whereby a Python object hierarchy is converted into a byte stream, and “unpickling” is the inverse operation, whereby a byte stream (from a binary file or bytes-like object) is converted back into an object hierarchy.


1 Answers

Decided to make it as an answer. pickle.load method expects to get a file like object, but you are providing a string instead, and therefore an exception. So instead of:

pickle.load('afile') 

you should do:

pickle.load(open('afile', 'rb')) 
like image 152
Rapolas K. Avatar answered Sep 25 '22 21:09

Rapolas K.