在python中加载对象时出现错误?

时间:2015-09-04 15:56:46

标签: python stream deserialization pickle dill

当我尝试在python中加载文件时,我从dill得到了这个错误:

for file in glob.glob('./SerializedData/Batch8202015_1999/*'):
    with open(file, 'rb') as stream:
        minibatch_test = dill.load(stream)

有人知道出了什么问题吗? 以下是发生错误的部分代码:

java.sql.Date format:

1 个答案:

答案 0 :(得分:2)

我是dill作者。这似乎不是序列化错误,它很可能是编码错误。

本质上,dispatch是一个pickle对象类型及其pickle函数的字典,其中键是对象类型。您有一个95作为密钥......这绝对不是dispatch所期望的。

>>> import dill
>>> dill.dill.pickle_dispatch_copy
{<type 'unicode'>: <function save_unicode at 0x10c8f1cf8>, <type 'dict'>: <function save_dict at 0x10c8f1f50>, <type 'int'>: <function save_int at 0x10c8f1b18>, <type 'long'>: <function save_long at 0x10c8f1b90>, <type 'list'>: <function save_list at 0x10c8f1e60>, <type 'str'>: <function save_string at 0x10c8f1c80>, <type 'function'>: <function save_global at 0x10c8f5140>, <type 'instance'>: <function save_inst at 0x10c8f50c8>, <type 'type'>: <function save_global at 0x10c8f5140>, <type 'NoneType'>: <function save_none at 0x10c8f1a28>, <type 'bool'>: <function save_bool at 0x10c8f1aa0>, <type 'tuple'>: <function save_tuple at 0x10c8f1d70>, <type 'float'>: <function save_float at 0x10c8f1c08>, <type 'classobj'>: <function save_global at 0x10c8f5140>, <type 'builtin_function_or_method'>: <function save_global at 0x10c8f5140>}

你有这样的事情:

>>> key = [95]
>>> dispatch = {}
>>> dispatch[key[0]]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 95

加载pickle对象的代码看起来很好......但是,虽然这可能是抛出错误的地方,但它并不是实际的错误。 没有足够的信息来自您上面提到的内容,以便发现它。