我尝试使用pickle
编写列表import pickle
pkl_file=('lol.txt','wb')
listoflist=[[1,2],[3,4],[5.6]]
pickle.dump(listoflist,pkl_file)
但出现以下错误:
Traceback (most recent call last):
File "lol.py", line 4, in <module>
pickle.dump(lol,pkl_file)
File "/usr/lib/python2.7/pickle.py", line 1370, in dump
Pickler(file, protocol).dump(obj)
File "/usr/lib/python2.7/pickle.py", line 203, in __init__
self.write = file.write
AttributeError: 'tuple' object has no attribute 'write'
我需要知道这有什么问题,还是有其他有效的方法呢?
答案 0 :(得分:4)
将pkl_file = open('lol.txt','wb')
代替('lol.txt','wb')
。
答案 1 :(得分:1)
此错误消息:
AttributeError: 'tuple' object has no attribute 'write'
是因为:
pkl_file=('lol.txt','wb')
不是你打算做的。你已经制作了两个字符串的元组。 将其更改为:
pkl_file = open('lol.txt','wb')