我有一个看起来像这样的列表:
a = [['a string', [0, 0, 0], [22, 'bee sting']], ['see string',
[0, 2, 0], [22, 'd string']]]
并且在保存和检索它时遇到问题。
我可以使用pickle保存它:
with open('afile','w') as f:
pickle.dump(a,f)
但是当我尝试加载它时出现以下错误:
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'
我曾认为我可以转换为 numpy 数组并使用save
,savez
或savetxt
. 但是我收到以下错误:
>>> 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