我正在使用ruamel.yaml库将字典转储到yaml文件中。然而,它的行为并不像我希望的那样。
我怎样才能得到:
---
- a:
- foo: bar
- bar: foo而不是:
a:
- foo: bar
- bar: foo使用yaml.dump()
我从以下位置提取数据:
data = {
'a': [
{'foo':'bar'},
{'bar':'foo'}
]
}任何其他具有相同功能的库都可以提交建议书。
发布于 2017-11-27 18:13:18
您要查找的是在YAML实例上设置explicit_start = True。此外,如果您想要- a而不仅仅是第二行上的a,则需要使顶级列表成为列表:
import sys
import ruamel.yaml
yaml_str = """\
---
-a:
-foo: bar
-bar: foo
"""
data = [{
'a': [
{'foo':'bar'},
{'bar':'foo'}
]
}]
yaml = ruamel.yaml.YAML()
yaml.explicit_start = True
yaml.dump(data, sys.stdout)提供:
---
- a:
- foo: bar
- bar: foohttps://stackoverflow.com/questions/47508088
复制相似问题