【问题标题】:Pickle Error when loading an object in python?在python中加载对象时出现Pickle错误?
【发布时间】:2015-09-04 15:56:46
【问题描述】:

当我尝试在 python 中加载文件时,我从 dill 收到此错误:

Traceback (most recent call last):
  File "LArSoftSGD.py", line 154, in <module>
    stat_bundle = train_batch_iterator(clf, TOTAL_TRAINED_EVENTS)
  File "LArSoftSGD.py", line 121, in train_batch_iterator
    plot_data.append((test_batch_iterator(clf), tte))
  File "LArSoftSGD.py", line 91, in test_batch_iterator
    minibatch_test = dill.load(stream)
  File "/home/jdoe/.local/lib/python3.4/site-packages/dill/dill.py", line 199, in load
obj = pik.load()
  File "/usr/lib/python3.4/pickle.py", line 1036, in load
dispatch[key[0]](self)
KeyError: 95

有人知道出了什么问题吗? 这是我发生错误的一些代码:

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

【问题讨论】:

    标签: python stream deserialization pickle dill


    【解决方案1】:

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

    本质上,调度是一个腌制对象类型及其腌制函数的字典,键是对象类型。你有一个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
    

    您加载腌制对象的代码看起来不错……但是,虽然这可能是引发错误的地方,但它并不是错误实际发生的地方。 您在上面提供的信息不足以发现这一点。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多