Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x YAML可转储递归defaultdict_Python 3.x_String_Recursion_Yaml_Defaultdict - Fatal编程技术网

Python 3.x YAML可转储递归defaultdict

Python 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) 我写信给的

我试图通过使用递归函数在任意级别上从返回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)
我写信给的文件的结果我希望看到:

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转储程序。