使用 Pickler 时出现 Python Pickle EOFerror(但不使用 pickle.dump())

发布于 2024-11-19 06:19:55 字数 1149 浏览 1 评论 0原文

因此,我尝试使用 Python 的 pickle 将一些对象保存到 Windows 7 上的磁盘。我正在使用下面的代码,它在几乎任何任意对象上都会失败(saveobj 的内容并不重要,无论如何它都会失败)。下面是我的测试代码:

import pickle, os, time
outfile = "foo.pickle"
f = open(outfile, 'wb')
p = pickle.Pickler(f, -1)
saveobj = ( 2,3,4,5,["hat", {"mat": 6}])
p.save(saveobj)
#pickle.dump(saveobj, f)
print "done pickling"
f.close()
g  = open(outfile, 'rb')
tup = pickle.load(g)
g.close()
print tup

当我运行它时,我得到以下输出/错误:

done pickling
Traceback (most recent call last):
  File "C:\Users\user\pickletest2.py", line 13, in <module>
    tup = pickle.load(g)
  File "C:\Python26\lib\pickle.py", line 1370, in load
    return Unpickler(file).load()
  File "C:\Python26\lib\pickle.py", line 858, in load
    dispatch[key](self)
  File "C:\Python26\lib\pickle.py", line 880, in load_eof
    raise EOFError
EOFError

但是,如果我使用 pickle.dump() 而不是 Pickler 对象,它就可以正常工作。我使用 Pickler 的原因是我想对其进行子类化,以便在 pickle 之前可以对每个对象执行操作。

有谁知道为什么我的代码这样做?我的搜索表明,没有“wb”和“rb”通常会导致这种情况,就像没有 f.close() 一样,但我两者都有。使用-1作为协议有问题吗?我想保留它,因为它可以处理定义自己的 __slots__ 方法的对象,而无需定义 __getstate__ 方法。

So, I'm trying to save some objects to disk on Windows 7 using Python's pickle. I'm using the code below, which fails on pretty much any arbitrary object (the contents of saveobj aren't important, it fails regardless). Below is my test code:

import pickle, os, time
outfile = "foo.pickle"
f = open(outfile, 'wb')
p = pickle.Pickler(f, -1)
saveobj = ( 2,3,4,5,["hat", {"mat": 6}])
p.save(saveobj)
#pickle.dump(saveobj, f)
print "done pickling"
f.close()
g  = open(outfile, 'rb')
tup = pickle.load(g)
g.close()
print tup

When I run it, I get the following output/error:

done pickling
Traceback (most recent call last):
  File "C:\Users\user\pickletest2.py", line 13, in <module>
    tup = pickle.load(g)
  File "C:\Python26\lib\pickle.py", line 1370, in load
    return Unpickler(file).load()
  File "C:\Python26\lib\pickle.py", line 858, in load
    dispatch[key](self)
  File "C:\Python26\lib\pickle.py", line 880, in load_eof
    raise EOFError
EOFError

However, if I use pickle.dump() instead of a Pickler object, it works just fine. My reason for using Pickler is that I would like to subclass it so I can perform operations on each object before I pickle it.

Does anybody know why my code is doing this? My searching has revealed that not having 'wb' and 'rb' commonly cause this, as does not having f.close(), but I have both of those. Is it a problem with using -1 as the protocol? I'd like to keep it, as it can handle objects which define their own __slots__ methods without defining a __getstate__ method.

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

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

发布评论

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

评论(2

雾里花 2024-11-26 06:19:55

Pickler.save() 是一个较低级别的方法,您不应该直接调用它。

如果您调用 p.dump(saveobj) 而不是 p.save(saveobj),它将按预期工作。

也许应该将其称为 _save 以避免混淆。但 dump 是文档中描述的方法,它与模块级 pickle.dump 完全匹配。

Pickler.save() is a lower level method, that you're not supposed to call directly.

If you call p.dump(saveobj) instead of p.save(saveobj), it works as expected.

Perhaps it should be called _save to avoid confusion. But dump is the method described in the documentation, and it neatly matches up with the module-level pickle.dump.

美人骨 2024-11-26 06:19:55

一般来说,出于性能原因,最好使用 cPickle(因为 cPickle 是用 C 编写的)。
无论如何,使用 dump 它工作得很好:

import pickle
import os, time
outfile = "foo.pickle"
f = open(outfile, 'wb')
p = pickle.Pickler(f, -1)
saveobj = ( 2,3,4,5,["hat", {"mat": 6}])
p.dump(saveobj)
#pickle.dump(saveobj, f)
f.close()
print "done pickling"
#f.close()
g  = open(outfile, 'rb')
u = pickle.Unpickler(g) #, -1)
tup = u.load()
#tup = pickle.load(g)
g.close()
print tup

In general it is better to use cPickle for performance reasons (since cPickle is written in C).
Anyway, using dump it works just fine:

import pickle
import os, time
outfile = "foo.pickle"
f = open(outfile, 'wb')
p = pickle.Pickler(f, -1)
saveobj = ( 2,3,4,5,["hat", {"mat": 6}])
p.dump(saveobj)
#pickle.dump(saveobj, f)
f.close()
print "done pickling"
#f.close()
g  = open(outfile, 'rb')
u = pickle.Unpickler(g) #, -1)
tup = u.load()
#tup = pickle.load(g)
g.close()
print tup
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文