pyyaml

In Python, how can you load YAML mappings as OrderedDicts?

本小妞迷上赌 提交于 2019-11-26 12:42:23
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 .

How can I control what scalar form PyYAML uses for my data?

浪子不回头ぞ 提交于 2019-11-26 09:40:54
问题 I\'ve got an object with a short string attribute, and a long multi-line string attribute. I want to write the short string as a YAML quoted scalar, and the multi-line string as a literal scalar: my_obj.short = \"Hello\" my_obj.long = \"Line1\\nLine2\\nLine3\" I\'d like the YAML to look like this: short: \"Hello\" long: | Line1 Line2 Line3 How can I instruct PyYAML to do this? If I call yaml.dump(my_obj) , it produces a dict-like output: {long: \'line1 line2 line3 \', short: Hello} (Not sure

In Python, how can you load YAML mappings as OrderedDicts?

旧街凉风 提交于 2019-11-26 03:04:24
问题 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? 回答1: 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