如何在 pkl 文件中编写可在 python 脚本中使用的 write() 方法?

发布于 2024-11-18 19:06:42 字数 1070 浏览 2 评论 0原文

我正在尝试在不同脚本之间共享布尔值。在一个脚本中,如果调用某个函数,我想编辑布尔值。在其他脚本中,我想使用布尔值。我正在尝试使用酸洗,但我无法理解。我不知道在我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

北方。的韩爷 2024-11-25 19:06:42

dumpload 所需的参数需要一个文件对象,因此您不能简单地将文件名作为字符串传递。 (并且您应该使用其他答案中提到的非 s 版本)

尝试如下操作: pickle.dump(boolean, open("filename.pkl", "w"))

>boolean = pickle.load(open("filename.pkl", "r"))

The arguments required for dump and load 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"))

空城仅有旧梦在 2024-11-25 19:06:42

首先,您需要使用 pickle.dump(...)pickle.load(),而不是字符串版本,就像这样,

import pickle

f = open('gherkin.pkl','w')
pickle.dump(False,f)
f.close()
g = open('gherkin.pkl','r')
print pickle.load(g)
g.close()

其次,如果您打开再次读取文件时,您需要将模式设置为“r”,否则您将破坏它。

First off, you need to use pickle.dump(...) and pickle.load(), not the string versions, like so,

import pickle

f = open('gherkin.pkl','w')
pickle.dump(False,f)
f.close()
g = open('gherkin.pkl','r')
print pickle.load(g)
g.close()

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.

寄意 2024-11-25 19:06:42

您使用了错误的 API。您使用的 dumpsloads 方法适用于字符串,而不是文件。 (名称中的 s 代表 string)。根据 文档,您应该使用 dumpload 使用文件对象。您应该事先打开文件对象。

You're using the wrong API. The dumps and loads methods you are using are for strings, not files. (The s in the name stands for string). According to the documentation you should be using dump and load with a file object. You should previously open the file object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文