2

我尝试使用 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'

我需要知道这有什么问题,或者还有其他有效的方法吗?

4

2 回答 2

4

pkl_file = open('lol.txt','wb')而不是('lol.txt','wb').

于 2013-04-16T22:20:23.683 回答
1

此错误消息:

AttributeError: 'tuple' object has no attribute 'write'

是因为:

pkl_file=('lol.txt','wb')

不是你打算做的。你已经创建了一个包含两个字符串的元组。将其更改为:

pkl_file = open('lol.txt','wb')
于 2013-04-16T22:24:01.033 回答