简体   繁体   English

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

[英]How do I write a write() method in a pkl file that I can use in my python scrpits?

I am trying a share a boolean between different scripts.我正在尝试在不同脚本之间共享 boolean。 In one script, I want to edit the boolean if a certain function is called.在一个脚本中,如果某个 function 被调用,我想编辑 boolean。 In the other scripts, I want to use the boolean.在其他脚本中,我想使用 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.我不知道在我的 pkl 文件中写什么。 My code kinda looks like this:我的代码有点像这样:

one.py一个.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二.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.我有另一个类似于 two.py 的脚本。 My pkl file is empty.我的 pkl 文件是空的。
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'当我尝试运行主脚本(与酸洗的完全不同的脚本)时,我得到“AttributeError:'str' object has no attribute 'write'

The arguments required for dump and load need a file object, so you cannot simply pass the filename as a string. dumpload所需的 arguments 需要一个文件 object,因此您不能简单地将文件名作为字符串传递。 (and you should use the non-s version as mentioned by other answers) (并且您应该使用其他答案中提到的非 s 版本)

Try something like this: pickle.dump(boolean, open("filename.pkl", "w"))尝试这样的事情: pickle.dump(boolean, open("filename.pkl", "w"))

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

First off, you need to use pickle.dump(...) and pickle.load() , not the string versions, like so,首先,您需要使用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()

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.其次,如果您再次打开文件以读取它,您需要将模式设置为"r" ,否则您将破坏它。

You're using the wrong API.您使用了错误的 API。 The dumps and loads methods you are using are for strings, not files.您使用的dumpsloads方法用于字符串,而不是文件。 (The s in the name stands for string ). (名称中的s代表string )。 According to the documentation you should be using dump and load with a file object.根据文档,您应该使用文件 object 进行dumpload You should previously open the file object.您应该事先打开文件 object。

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

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