如何在YAML格式的文件中写入数据? [英] How can I write data in YAML format in a file?
问题描述
{A:a,B:{C: c,D:d,E:e}}
即词典中的字典。
import yaml
data = dict(
A ='a',
B = dict(
C ='c',
D ='d',
E ='e ',
)
)
打开('data.yml','w')作为outfile:
yaml.dump(data,outfile,default_flow_style = False)
I need to write the below data to yaml file using Python: i.e., dictionary in a dictionary. How can I achieve this? The
这篇关于如何在YAML格式的文件中写入数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
$ $ p $ $ code $ A $ a
B:{C:c,D:d,E:e}
{A:a, B:{C:c, D:d, E:e}}
import yaml
data = dict(
A = 'a',
B = dict(
C = 'c',
D = 'd',
E = 'e',
)
)
with open('data.yml', 'w') as outfile:
yaml.dump(data, outfile, default_flow_style=False)
default_flow_style=False
parameter is necessary to produce the format you want (flow style), otherwise for nested collections it produces block style:A: a
B: {C: c, D: d, E: e}