When i dump my yaml data using yaml.dump(stream_data)
I'm getting below output
models:
- model: "a"
type: "x"
groups:[]
#bunch of properties...
- model: "b"
type: "y"
groups: []
#bunch of properties...
desired output:
models:
- { model: "a", type: "x", groups:[]}
- { model: "b", type: "y", groups: []}
What you want is two level of flow-style nodes and in ruamel.yaml the only way to create that is force flow-style on the nested dicts:
import sys
import ruamel.yaml
def flow_dict(**kw):
result = ruamel.yaml.comments.CommentedMap(**kw)
result.fa.set_flow_style()
return result
data = dict(models=[flow_dict(model='a', type='x', groups=[]), flow_dict(model='b', type='y', groups=[])])
yaml = ruamel.yaml.YAML()
yaml.indent(sequence=4, offset=2)
yaml.dump(data, sys.stdout)
which gives:
models:
- {model: a, type: x, groups: []}
- {model: b, type: y, groups: []}
There is no way (apart from post-processing the output stream), to get the space between {
and the model
key.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.