I want to dump a Python object to a YAML file like this:
a: 5
b: 6
c: 7
d: [ 1,2,3,4]
but NOT
a: 5
b: 6
c: 7
d
- 1
- 2
- 3
- 4
image d was a massive list, the output gets messy for humans to see.
I use: default_flow_style=False
but this uses the new line list item format.
I already use a customer dumper to stop anchors.
sbarzowski's comment worked.
I have tested for python 3.9, with PyYaml 5.4.1
Use yaml.dump
with default_flow_style=None
, to get the desired effect.
You need to give the dictionary you are trying to dump.
yaml.dump() will Just Work depending on the dictionary you have.
%python u
first: [1, 2, 3]
second: bar
%cat u
import yaml
d = { "first" : [1,2,3], "second" : "bar" }
print (yaml.dump(d))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With