Python 3.x YAML可转储递归defaultdict
我试图通过使用递归函数在任意级别上从返回defaultdict来构造yaml文档。Python 3.8.2Python 3.x YAML可转储递归defaultdict,python-3.x,string,recursion,yaml,defaultdict,Python 3.x,String,Recursion,Yaml,Defaultdict,我试图通过使用递归函数在任意级别上从返回defaultdict来构造yaml文档。Python 3.8.2 from collections import defaultdict import yaml def ret_dd(): return defaultdict(ret_dd) yml = ret_dd() yml['a']['b']['c'] = 1 with open('newfile.txt','w') as f: yaml.dump(yml, f) 我写信给的
from collections import defaultdict
import yaml
def ret_dd():
return defaultdict(ret_dd)
yml = ret_dd()
yml['a']['b']['c'] = 1
with open('newfile.txt','w') as f:
yaml.dump(yml, f)
我写信给的文件的结果我希望看到:
a:
b:
c: 1
然而,我看到的是:
!!python/object/apply:collections.defaultdict
args:
- &id001 !!python/name:__main__.ret_dd ''
dictitems:
a: !!python/object/apply:collections.defaultdict
args:
- *id001
dictitems:
b: !!python/object/apply:collections.defaultdict
args:
- *id001
dictitems:
c: !!python/object/apply:collections.defaultdict
args:
- *id001
我缺少哪一步?您可以使用或转换为正常dict,然后运行yaml转储程序。您可以使用或转换为正常dict,然后运行yaml转储程序。