I'd like to get PyYAML's loader to load mappings (and ordered mappings) into the Python 2.7+ OrderedDict type, instead of the vanilla dict
and the list of pairs it currently uses.
What's the best way to do that?
I'd like to get PyYAML's loader to load mappings (and ordered mappings) into the Python 2.7+ OrderedDict type, instead of the vanilla dict
and the list of pairs it currently uses.
What's the best way to do that?
Update: In python 3.6+ you probably don't need
OrderedDict
at all due to the new dict implementation that has been in use in pypy for some time (although considered CPython implementation detail for now).Update: In python 3.7+, the insertion-order preservation nature of dict objects has been declared to be an official part of the Python language spec, see What's New In Python 3.7.
I like @James' solution for its simplicity. However, it changes the default global
yaml.Loader
class, which can lead to troublesome side effects. Especially, when writing library code this is a bad idea. Also, it doesn't directly work withyaml.safe_load()
.Fortunately, the solution can be improved without much effort:
For serialization, I don't know an obvious generalization, but at least this shouldn't have any side effects:
2018 option:
oyaml
is a drop-in replacement for PyYAML which preserves dict ordering. Both Python 2 and Python 3 are supported. Justpip install oyaml
, and import as shown below:You'll no longer be annoyed by screwed-up mappings when dumping/loading.
Note: I'm the author of oyaml.
2015 (and later) option:
ruamel.yaml is a drop in replacement for PyYAML (disclaimer: I am the author of that package). Preserving the order of the mappings was one of the things added in the first version (0.1) back in 2015. Not only does it preserve the order of your dictionaries, it will also preserve comments, anchor names, tags and does support the YAML 1.2 specification (released 2009)
The specification says that the ordering is not guaranteed, but of course there is ordering in the YAML file and the appropriate parser can just hold on to that and transparently generate an object that keeps the ordering. You just need to choose the right parser, loader and dumper¹:
will give you:
data is of type CommentedMap which functions like a dict, but has extra information that is kept around until being dumped (including the preserved comment!)