0

I was able to save SQL in beauty way based on question Save SQL to YAML as is.

Now, I should resolve issue with saving order in YAML as was before loading. So, I had YAML config as:

app:
  ....
conf:
  sql:
    ....
  mapping:
    ....
filters:
  ....

Once I save with ruamel.yaml I got random order saving as example below:

filters:
  ....
conf:
  mapping:
    ....
  sql:
    ....
app:
  ....

How can I save the keys of YAML-file as was loaded?

Solution: https://stackoverflow.com/a/30763929/4437954

Community
  • 1
  • 1
SpanishBoy
  • 2,105
  • 6
  • 28
  • 51
  • YAML mappings and Python dictionaries are *unordered*. Use lists if order is important. See the [YAML mapping specification](http://yaml.org/spec/1.1/#id932806): *A mapping node is an **unordered** collection of key: value pairs*. – Martijn Pieters Jun 10 '15 at 17:50
  • Ah, it appears you can work around this by forcing the YAML parser to use OrderedDict objects; an `OrderedDict` lists contents in the order that you added keys. See the dupe. – Martijn Pieters Jun 10 '15 at 17:53
  • You can tell YAML to load mappings as OrderedDicts. See [this answer](http://stackoverflow.com/a/21912744/866026) for how to do that. – Waylan Jun 10 '15 at 17:54

0 Answers0