Python pickle / deselecciona una lista a / desde un archivo
Tengo una lista que se parece a esto:
a = [['a string', [0, 0, 0], [22, 'bee sting']], ['see string',
[0, 2, 0], [22, 'd string']]]
y estoy teniendo problemas para guardarlo y recuperarlo.
Puedo guardarlo bien usando pickle:
with open('afile','w') as f:
pickle.dump(a,f)
pero obtengo el siguiente error cuando intento cargarlo:
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'
Pensé que podría convertir a una matriz numpy y usarsave
, savez
osavetxt
. Sin embargo me sale el siguiente 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