我将数据存储在以下对象中:
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
我要使用python3,并且难以使用pickle
进行存储:problem loading Autovivification file when moving from python 2.7 to 3.6, KeyError: 'DictType'。我正在寻找可以存储该对象的东西。我尝试过:
import pandas as pd
import jsonpickle
import h5py
def test_hdf5():
dogs = AutoVivification()
dogs['type'] = pd.DataFrame([['k9']])
dogs['category']['color'] = 'white'
dogs['#'] = 4
hf = h5py.File(path_test + 'test.h5', 'w')
hf.create_dataset('dataset_1', data=dogs)
hf.close()
def test_jsonpickle():
dogs = AutoVivification()
dogs['type'] = pd.DataFrame([['k9']])
dogs['category']['color'] = 'white'
dogs['#'] = 4
frozen = jsonpickle.encode(dogs)
thawed = jsonpickle.decode(frozen)
这两个都不起作用。
有什么建议可以尝试吗?