26

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
Saullo G. P. Castro
  • 56,802
  • 26
  • 179
  • 234
Lee
  • 29,398
  • 28
  • 117
  • 170
  • 6
    Instead of pickle.load('afile') do pickle.load(open('afile')) – Rapolas K. Aug 14 '13 at 10:21
  • Of course! thanks - Why don't you put that as an answer.. – Lee Aug 14 '13 at 10:23
  • You can't easily convert what you have to a `numpy.array` as it has an irregular shape. You'd have to make a big regular array and then fill in all the data with `NaN` or something like that… essentially what `pandas` does for you. It's probably overkill for your small list, however. – Mike McKerns Jun 11 '14 at 21:14

2 Answers2

43

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'))
jonny
  • 4,264
  • 4
  • 22
  • 29
Rapolas K.
  • 1,669
  • 26
  • 34
  • 2
    maybe `open('afile', 'rb')`. Note from the `pickle` documentation: *Be sure to always open pickle files created with protocols >= 1 in binary mode.* – cdarke Aug 14 '13 at 11:05
18

To add to @ Rapolas K's answer:

I found that I had problems with the file not closing so used this method:

with open('afile','rb') as f:
     pickle.load(f)
Lee
  • 29,398
  • 28
  • 117
  • 170