我有这个嵌套字典类,我需要将其实例转储到 YAML
class NestedDict(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
在转储这本词典时:
pyaml.dump(nesteddict)
我收到此错误(仅发布了整个消息对象的摘录):
“RepresenterError:无法表示对象:{'a1401': 'ts755', 'ts64': {'topic': {'a1561': 'Process Control'}}, 'a1450': 'ts107', 'a1609': 'ts341','a1400':'ts753',......
那么如何在 YAML 中巧妙地表示这一点?我读到 PyYAML 确实支持嵌套递归结构。