5

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}
Quuxplusone
  • 19,419
  • 5
  • 72
  • 137

2 Answers2

5

This:

import sys
import ruamel.yaml

mydict = dict(a1=1, a2=2, a3=3, a11=11, a21=21)
ruamel.yaml.round_trip_dump(mydict, sys.stdout)

gives non-sorted output. On my system:

a11: 11
a2: 2
a21: 21
a3: 3
a1: 1

by adding:

my_sorted_dict = ruamel.yaml.comments.CommentedMap()
for k in sorted(mydict):
    my_sorted_dict[k] = mydict[k]
ruamel.yaml.round_trip_dump(my_sorted_dict, sys.stdout)

this will be sorted:

a1: 1
a11: 11
a2: 2
a21: 21
a3: 3

The commented map is the structure ruamel.yaml uses when doing a round-trip (load+dump) and round-tripping is designed to keep the keys in the order that they were before.

If you loaded mydict from a YAML file and only need to add a few keys, you can also walk over mydict's keys and insert (mydict.insert(pos, new_key, new_value)) the key value pairs.

Of course you can get the same output by doing a normal safe_dump() if you don't need any of the other special features:

ruamel.yaml.safe_dump(mydict, sys.stdout, allow_unicode=True, 
                      default_flow_style=False)
Anthon
  • 51,019
  • 25
  • 150
  • 211
  • Oho, is `default_flow_style=False` the appropriate way to enable "Kubernetes-style" YAML output? I may give that a try. – Quuxplusone Oct 24 '16 at 21:51
  • This is kind of a separate question, and I can ask it separately if you like, but now I'm wondering: is there a nice way to specify "sort keys by default, but if there's a key named `name`, put it first", again applying recursively to the whole structure? If there were such a way, I would use it. – Quuxplusone Oct 24 '16 at 22:03
  • @Quuxplusone Just put the (`name`, value) pair in there first, or use `.insert()` as I indicated. You can also write a specific serializer that knows about `name`, but because of the way PyYAML was implemented (and ruamel.yaml still follows) it is difficult to parametrize this for any key (and that answer would require a seperate question). – Anthon Oct 25 '16 at 05:55
  • 1
    @Anthon how would you recurse nested a CommentedMap to sort all keys at all levels? – SkunkSpinner Jul 16 '18 at 21:25
  • @SkunkSpinner I would use a recursive function that handles the three node types: map, sequence and scallar and then probably sort the map in place, so the end-of-line comments are preserved without extra work. Post another question if you need more detail (tag it [ruamel.yaml] and I'll get notified) – Anthon Jul 17 '18 at 04:34
0

There is an undocumented sort() in ruamel.yaml that will work on a variation of this problem:

import sys
import ruamel.yaml

yaml = ruamel.yaml.YAML()

test = """- name: a11
  value: 11
- name: a2
  value: 2
- name: a21
  value: 21
- name: a3
  value: 3
- name: a1
  value: 1"""
test_yml = yaml.load(test)

yaml.dump(test_yml, sys.stdout)

not sorted output

  - name: a11
    value: 11
  - name: a2
    value: 2
  - name: a21
    value: 21
  - name: a3
    value: 3
  - name: a1
    value: 1

sort by name

test_yml.sort(lambda x: x['name'])
yaml.dump(test_yml, sys.stdout)

sorted output

  - name: a1
    value: 1
  - name: a11
    value: 11
  - name: a2
    value: 2
  - name: a21
    value: 21
  - name: a3
    value: 3
yingw
  • 147
  • 6