I want to laod a YAML-file to a dict. Everything I found is outdated, since Python 3.6 changed dicts to be ordered by insertion. What would be the proper way to do it?
Asked
Active
Viewed 268 times
1
-
Would list `[(index, line)]` make sense? – Zazaeil May 30 '20 at 11:09
-
You could have a look at OrderedDict for this purpose: https://stackoverflow.com/questions/16782112/can-pyyaml-dump-dict-items-in-non-alphabetical-order – Nico Müller May 30 '20 at 11:11
-
@SerejaBogolubov My dict is nested. I'm not really sure whether this would work. I would prefer a dict, especially since they preserve order now. I want to read the yaml, change some values and then dump it again. I think this would be easiest with a dict. (Please make different suggestions if you think, they would work) – waldelb May 30 '20 at 11:22
-
@NicoMüller What is the difference between Dict and OrderedDict, now that dicts are ordered by insertion? – waldelb May 30 '20 at 11:23
1 Answers
0
It is possible to do that with ruamel.
import ruamel.yaml
yaml = ruamel.yaml.YAML(typ='safe')
with open(path_to_file) as file:
my_dict = yaml.load(file)

waldelb
- 705
- 6
- 11