When I try to pickle something, I get an AttributeError: 'str' object has no attribute 'write'
当我尝试腌制某些东西时,我得到一个AttributeError:'str'对象没有属性'write'
An example:
一个例子:
import pickle
pickle.dump({"a dict":True},"a-file.pickle")
produces:
生产:
...
AttributeError: 'str' object has no attribute 'write'
What's wrong?
怎么了?
1 个解决方案
#1
61
It's a trivial mistake: pickle.dump(obj,file)
takes a file
object, not a file name.
这是一个微不足道的错误:pickle.dump(obj,file)接受一个文件对象,而不是文件名。
What I need is something like:
我需要的是:
with open("a-file.pickle",'wb') as f:
pickle.dump({"a dict":True},f)
#1
61
It's a trivial mistake: pickle.dump(obj,file)
takes a file
object, not a file name.
这是一个微不足道的错误:pickle.dump(obj,file)接受一个文件对象,而不是文件名。
What I need is something like:
我需要的是:
with open("a-file.pickle",'wb') as f:
pickle.dump({"a dict":True},f)