使用 Pickler 时出现 Python Pickle EOFerror(但不使用 pickle.dump())
因此,我尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
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 ofp.save(saveobj)
, it works as expected.Perhaps it should be called
_save
to avoid confusion. Butdump
is the method described in the documentation, and it neatly matches up with the module-levelpickle.dump
.一般来说,出于性能原因,最好使用 cPickle(因为 cPickle 是用 C 编写的)。
无论如何,使用 dump 它工作得很好:
In general it is better to use cPickle for performance reasons (since cPickle is written in C).
Anyway, using dump it works just fine: