problem loading Autovivification file when moving from python 2.7 to 3.6, KeyError: 'DictType'

倾然丶 夕夏残阳落幕 提交于 2019-12-02 11:43:13

问题


In python 2.7, I have a bunch of files stored as the following object:

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

This is from What is the best way to implement nested dictionaries?.

I have them pickled and can load them just fine. But in python 3.6, it gives me the following error when trying to load the same file.

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'

I'm using the following line of code to load the object:

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

how do I use python 3.6 to load the file?

来源:https://stackoverflow.com/questions/54622935/problem-loading-autovivification-file-when-moving-from-python-2-7-to-3-6-keyerr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!