1

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 を使用してファイルをロードするにはどうすればよいですか?

4

0 に答える 0