在 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
这是来自实现嵌套字典的最佳方法是什么?.
我把它们腌制了,可以很好地加载它们。但在 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 加载文件?