I'm trying to dump a Python dict to a YAML file using ruamel.yaml
. I'm familiar with the json
module's interface, where pretty-printing a dict is as simple as
import json
with open('outfile.json', 'w') as f:
json.dump(mydict, f, indent=4, sort_keys=True)
With ruamel.yaml
, I've gotten as far as
import ruamel.yaml
with open('outfile.yaml', 'w') as f:
ruamel.yaml.round_trip_dump(mydict, f, indent=2)
but it doesn't seem to support the sort_keys
option. ruamel.yaml
also doesn't seem to have any exhaustive docs, and searching Google for "ruamel.yaml sort" or "ruamel.yaml alphabetize" didn't turn up anything at the level of simplicity I'd expect.
Is there a one-or-two-liner for pretty-printing a YAML file with sorted keys?
(Note that I need the keys to be alphabetized down through the whole container, recursively; just alphabetizing the top level is not good enough.)
Notice that if I use round_trip_dump
, the keys are not sorted; and if I use safe_dump
, the output is not "YAML-style" (or more importantly "Kubernetes-style") YAML. I don't want []
or {}
in my output.
$ pip freeze | grep yaml
ruamel.yaml==0.12.5
$ python
>>> import ruamel.yaml
>>> mydict = {'a':1, 'b':[2,3,4], 'c':{'a':1,'b':2}}
>>> print ruamel.yaml.round_trip_dump(mydict) # right format, wrong sorting
a: 1
c:
a: 1
b: 2
b:
- 2
- 3
- 4
>>> print ruamel.yaml.safe_dump(mydict) # wrong format, right sorting
a: 1
b: [2, 3, 4]
c: {a: 1, b: 2}
default_flow_style=False
the appropriate way to enable "Kubernetes-style" YAML output? I may give that a try. – Oliverolivera