如何在 pkl 文件中编写可在 python 脚本中使用的 write() 方法?
我正在尝试在不同脚本之间共享布尔值。在一个脚本中,如果调用某个函数,我想编辑布尔值。在其他脚本中,我想使用布尔值。我正在尝试使用酸洗,但我无法理解。我不知道在我的 pkl 文件中写什么。我的代码有点像这样:
one.py
import pickle
boolean = False
pickle.dumps(boolean, "filename.pkl")
class Foo(object):
#init method irrelevant
def bar(self):
foobar = raw_input("> ")
if foobar == "baz":
boolean = True
pkl_file = open("filename.pkl", 'w')
pickle.dumps(boolean, "filename.pkl")
else:
print "Hello"
Two.py
import pickle
class Foobar(object):
#init method irrelevant
def foo_bar(self):
foobar = raw_input("> ")
boolean = pickle.loads("filename.pkl")
if foobar == "foo" and boolean:
print "Hi!"
elif foobar == "foo":
print "Hello there."
else:
print "Bye!"
我有另一个脚本,它执行类似于two.py 的操作。我的 pkl 文件是空的。
当我尝试运行主脚本(与酸洗脚本完全不同)时,我得到“AttributeError:'str'对象没有属性'write'
I am trying a share a boolean between different scripts. In one script, I want to edit the boolean if a certain function is called. In the other scripts, I want to use the boolean. I'm trying to use pickling, but I'm in way over my head. I have no idea what to write in my pkl file. My code kinda looks like this:
one.py
import pickle
boolean = False
pickle.dumps(boolean, "filename.pkl")
class Foo(object):
#init method irrelevant
def bar(self):
foobar = raw_input("> ")
if foobar == "baz":
boolean = True
pkl_file = open("filename.pkl", 'w')
pickle.dumps(boolean, "filename.pkl")
else:
print "Hello"
two.py
import pickle
class Foobar(object):
#init method irrelevant
def foo_bar(self):
foobar = raw_input("> ")
boolean = pickle.loads("filename.pkl")
if foobar == "foo" and boolean:
print "Hi!"
elif foobar == "foo":
print "Hello there."
else:
print "Bye!"
I have another script that does something similar to two.py. My pkl file is empty.
When I try to run the main script (a completely different one from the ones with pickling), I get "AttributeError: 'str' object has no attribute 'write'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
dump
和load
所需的参数需要一个文件对象,因此您不能简单地将文件名作为字符串传递。 (并且您应该使用其他答案中提到的非 s 版本)尝试如下操作:
pickle.dump(boolean, open("filename.pkl", "w"))
和
>boolean = pickle.load(open("filename.pkl", "r"))
The arguments required for
dump
andload
need a file object, so you cannot simply pass the filename as a string. (and you should use the non-s version as mentioned by other answers)Try something like this:
pickle.dump(boolean, open("filename.pkl", "w"))
and
boolean = pickle.load(open("filename.pkl", "r"))
首先,您需要使用
pickle.dump(...)
和pickle.load()
,而不是字符串版本,就像这样,其次,如果您打开再次读取文件时,您需要将模式设置为
“r”
,否则您将破坏它。First off, you need to use
pickle.dump(...)
andpickle.load()
, not the string versions, like so,Secondly, if you open the file a second time to read it you need to set the mode to
"r"
, otherwise you're going to destroy it.您使用了错误的 API。您使用的
dumps
和loads
方法适用于字符串,而不是文件。 (名称中的s
代表string
)。根据 文档,您应该使用dump
和load
使用文件对象。您应该事先打开文件对象。You're using the wrong API. The
dumps
andloads
methods you are using are for strings, not files. (Thes
in the name stands forstring
). According to the documentation you should be usingdump
andload
with a file object. You should previously open the file object.