When serializing an OrderedDict
with yaml.dump()
the output is quite difficult to comprehend due to the many dashes:
refine: !!python/object/apply:collections.OrderedDict
- - - root
- Wuhan/Hu-1/2019
- - clock_rate
- 0.0007
- - clock_std_dev
- 0.0003
Is there a way to make yaml.dump()
serialize OrderedDict
s just like normal Dict
s? What I want is the much more readable:
refine:
root: Wuhan/Hu-1/2019
clock_rate: 0.0007
clock_std_dev: 0.0003
Do I need to iterate through the entire variable (it's composed of hundreds of dicts, the above is just an excerpt) and cast all OrderedDict
s as Dict
s or is there an in-built feature of yaml.dump()
or a similar library that does this for me?
Using ruamel.yaml
as drop in replacement for PyYAML
solved the problem instantly. OrderedDict
s are no longer represented as lists in the output.
This code:
import ruamel.yaml
yaml=ruamel.yaml.YAML()
yaml.dump()
Produces the much neater output:
refine: !!omap
- root: Wuhan/Hu-1/2019
- clock_rate: 0.0007
- clock_std_dev: 0.0003