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
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.
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.
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.
“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.
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'))
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