代码之家  ›  专栏  ›  技术社区  ›  jason

从Python2.7移到3.6时加载自动激活文件时出现问题,KeyError:'DictType'

  •  1
  • jason  · 技术社区  · 6 年前

    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?

    我把它们腌制好了,可以很好地装进去。但在Python3.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)
    

    0 回复  |  直到 6 年前