简体   繁体   English

python pickle给出“AttributeError:'str'对象没有属性'write'”

[英]python pickle gives “AttributeError: 'str' object has no attribute 'write'”

When I try to pickle something, I get an AttributeError: 'str' object has no attribute 'write' 当我尝试腌制某些东西时,我得到一个AttributeError: 'str' object has no attribute 'write'

An example: 一个例子:

import pickle
pickle.dump({"a dict":True},"a-file.pickle")

produces: 生产:

...
AttributeError: 'str' object has no attribute 'write'

What's wrong? 怎么了?

It's a trivial mistake: pickle.dump(obj,file) takes a file object, not a file name. 这是一个微不足道的错误: pickle.dump(obj,file)接受一个file对象,而不是文件名。

What I need is something like: 我需要的是:

with open("a-file.pickle",'wb') as f:
    pickle.dump({"a dict":True},f)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM