3

我的问题是自定义类已与 pickle.dump 一起保存,因为这些文件已保存自定义类已更改,现在当我使用 pickle.load 时出现此错误。是不是保存的文件有问题?

错误:

File "/cprprod/extern/lib/python2.7/pickle.py", line 1378, in load
return Unpickler(file).load()
File "/cprprod/extern/lib/python2.7/pickle.py", line 858, in load
dispatch[key](self)
file "/cprprod/extern/lib/python2.7/pickle.py", line 1070, in load_inst
self._instantiate(klass, self.marker())
File "/cprprod/extern/lib/python2.7/pickle.py", line 1060, in _instantiate
value = klass(*args)

我可以做些什么来加载文件吗?

编码

file = open(filename,'rb')
obj = pickle.load(file)

会给我错误。


这是一些可以重现错误的最小代码:

import pickle

class foo:
    def __init__(self,a):
        self.a = a

    def __str__(self):
        return str(self.a)

obj = foo(1)

with open('junk','wb') as f:
    pickle.dump(obj,f)

class foo:
    def __init__(self,a,b):
        self.a = a
        self.b = b
    def __str__(self):
        return '%s %s'%(self.a,self.b)

    def __getinitargs__(self):
        return (self.a,self.b)

with open('junk','rb') as f:
    obj = pickle.load(f)
    print str(obj)
4

3 回答 3

2

If you added __getinitargs__() then it is up to you to make sure your new class can handle the arguments passed to __init__(). Old data that doesn't have the __getinitargs__ data will still lead to __init__ to be called but with no arguments.

Make the arguments to __init__ optional via keyword arguments:

def __init__(self, otherarg=None):
    if otherarg is None:
        # created from an old-revision pickle. Handle separately.
        # The pickle will be loaded *normally* and data will still be set normally
        return
    self.otherarg = otherarg

When loading the old-style pickle, the data for these classes will still be restored. You can use __setstate__() to transform the internal state as needed.

Alternatively, temporarily remove the __getinitargs__ method from the class:

initargs = foo.__getinitargs__.__func__
del foo.__getinitargs__
obj = pickle.load(f)
foo.__getinitargs__ = initargs

and re-dump your pickles from the now-loaded objects with __getinitargs__ reinstated.

I've tested both methods and in both cases the old data is loaded correctly and you can then dump your objects again to a new pickle file with __getinitargs__ just fine.

于 2013-01-09T15:53:34.590 回答
2

鉴于我代表您在问题中发布的人为代码,我们可以将这个错误“修复”为:

with open('junk','rb') as f:
    try:
        obj = pickle.load(f)
    except Exception as e:
        print e
        position = f.tell()
        a = foo.__getinitargs__
        del foo.__getinitargs__
        f.seek(position)
        obj = pickle.load(f)
        foo.__getinitargs__ = a

    print str(obj)

现在我们看到该实例已取消腌制并且不再具有属性b

于 2013-01-09T16:07:24.813 回答
1

您可能希望修改自定义类以选择性地需要第二个参数。这将保持与您的腌制对象的奖励兼容性。

于 2013-01-09T15:24:22.860 回答