I\'m using yaml.dump
to output a dict. It prints out each item in alphabetical order based on the key.
>>> d = {\"z\":0,\"y\":0,\"x\":
There are two things you need to do to get this as you want:
dict
, because it doesn't keep the items orderedimport sys
import ruamel.yaml
from ruamel.yaml.comments import CommentedMap
d = CommentedMap()
d['z'] = 0
d['y'] = 0
d['x'] = 0
ruamel.yaml.round_trip_dump(d, sys.stdout)
output:
z: 0
y: 0
x: 0
¹ This was done using ruamel.yaml a YAML 1.2 parser, of which I am the author.