Python Pickle 在读取和未正确读取时产生 EOF 错误

发布于 2024-12-09 10:37:45 字数 582 浏览 1 评论 0原文

我正在尝试使用以下方法来腌制患者对象:

theFile = open(str(location)+str(filename)+'.pkl','wb')
pickle.dump(self,theFile)
theFile.close()

这效果很好,并且可以根据需要成功写入文件。但!当我尝试从拇指加载数据时,出现 EOF 错误 XOR 它加载拇指中不存在的旧数据。我不知道这些旧数据来自哪里,考虑到 pickle 包含所有正确的保存数据...

加载操作:

theFile = open('/media/SUPER/hr4e/thumb/patient.pkl','r+')
self = pickle.load(theFile)
theFile.close()

一个例子是:我更改所需对象的属性并保存它。该属性显然保存在 pickle 文件中,但是当我在另一台计算机上重新加载 pickle 文件时,它不会读取 pickle 并加载旧数据。我检查了一下它是否正在读取泡菜,它是......

我错过了关于泡菜的任何微妙的细微差别吗?或者,我是否只是使用错误的读写参数来保存和加载泡菜?

I am trying to pickle a patient object by using:

theFile = open(str(location)+str(filename)+'.pkl','wb')
pickle.dump(self,theFile)
theFile.close()

This works well and successfully writes to the file as desired. But! When I try to load the data from the thumb, I get an EOF error XOR it loads old data that is not present in the thumb. I don't know where this old data is coming from, considering the pickle contains all the correct saved data...

Loading operation:

theFile = open('/media/SUPER/hr4e/thumb/patient.pkl','r+')
self = pickle.load(theFile)
theFile.close()

An example would be: I change an attribute of the desired object and save it. The attribute is clearly saved in the pickle file, but when I reload the pickle file on another computer, it doesn't read the pickle and loads old data. I checked to see if it was reading the pickle and it is...

Are there any subtle nuances about pickles that I am missing? Or, am I just using the wrong read and write arguments for the pickle saving and loading?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

梦归所梦 2024-12-16 10:37:45

在方法内分配给 self 只会更新变量 self 在该方法中指向的内容;它不会更新对象本身。要加载它,请从类方法或函数返回新加载的对象。尝试这样的代码:

import pickle
class Patient(object):
    def __init__(self, name):
        self.name = name

    def save(self, location, filename):
        theFile = open(str(location)+str(filename)+'.pkl','wb')
        pickle.dump(self,theFile)
        theFile.close()

    @classmethod
    def load(cls, location, filename):
        theFile = open(str(location)+str(filename)+'.pkl','rb')
        m = pickle.load(theFile)
        theFile.close()
        return m

p = Patient("Bob")
print p.name

# save the patient
p.save("c:\\temp\\", "bob")

# load the patient - this could be in a new session
l = Patient.load("c:\\temp\\", "bob")
print l.name

Assigning to self inside a method only updates what the variable self points to in that method; it doesn't update the object itself. To load it, instead return a newly loaded object from a classmethod or function. Try code like this:

import pickle
class Patient(object):
    def __init__(self, name):
        self.name = name

    def save(self, location, filename):
        theFile = open(str(location)+str(filename)+'.pkl','wb')
        pickle.dump(self,theFile)
        theFile.close()

    @classmethod
    def load(cls, location, filename):
        theFile = open(str(location)+str(filename)+'.pkl','rb')
        m = pickle.load(theFile)
        theFile.close()
        return m

p = Patient("Bob")
print p.name

# save the patient
p.save("c:\\temp\\", "bob")

# load the patient - this could be in a new session
l = Patient.load("c:\\temp\\", "bob")
print l.name
〆凄凉。 2024-12-16 10:37:45

以二进制模式打开文件。例如
theFile = open('/media/SUPER/hr4e/thumb/patent.pkl','r+b')

Open the file in binary mode. e.g.
theFile = open('/media/SUPER/hr4e/thumb/patient.pkl','r+b')

停滞 2024-12-16 10:37:45

我最终腌制了对象的属性字典。这样效果好多了。例子:

self.__dict__ = pickle.load(file('data/pickles/clinic.pkl','r+b'))

I ended up pickling the object's attributes dictionary. That worked much better. Example:

self.__dict__ = pickle.load(file('data/pickles/clinic.pkl','r+b'))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文