Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python yaml.dump format list in other YAML format

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.

like image 519
Gordon Avatar asked May 17 '14 22:05

Gordon


2 Answers

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.

like image 51
aahnik Avatar answered Sep 17 '22 00:09

aahnik


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))
like image 40
Bob Smeldyn Avatar answered Sep 18 '22 00:09

Bob Smeldyn