从python 2.7移至3.6时,加载Autovivification文件时出现问题,KeyError:'DictType'

时间:2019-02-11 01:38:53

标签: python dictionary autovivification

在python 2.7中,我有一堆文件存储为以下对象:

class AutoVivification(dict):
    """Implementation of perl's autovivification feature."""

    def __getitem__(self, item):
        try:
            return dict.__getitem__(self, item)
        except KeyError:
            value = self[item] = type(self)()
            return value

这来自What is the best way to implement nested dictionaries?

我把它们腌制了,可以很好地装载它们。但是在python 3.6中,尝试加载相同文件时会出现以下错误。

Traceback (most recent call last):
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2018.3.1\helpers\pydev\_pydevd_bundle\pydevd_exec2.py", line 3, in Exec
    exec(exp, global_vars, local_vars)
  File "<string>", line 2, in <module>
  File "C:\Python36\lib\site-packages\dill\_dill.py", line 577, in _load_type
    return _reverse_typemap[name]
KeyError: 'DictType'

我正在使用以下代码行加载对象:

with open('data.pkl', 'rb') as f:
    return pickle.load(f)

如何使用python 3.6加载文件?

0 个答案:

没有答案