ruamel.yaml是否相当于sort_keys? [英] ruamel.yaml equivalent of sort_keys?
问题描述
我正在尝试使用ruamel.yaml
将Python字典转储到YAML文件中.我熟悉json
模块的界面,在其中漂亮地打印字典就像
I'm trying to dump a Python dict to a YAML file using ruamel.yaml
. I'm familiar with the json
module's interface, where pretty-printing a dict is as simple as
import json
with open('outfile.json', 'w') as f:
json.dump(mydict, f, indent=4, sort_keys=True)
有了ruamel.yaml
,我已经达到了
import ruamel.yaml
with open('outfile.yaml', 'w') as f:
ruamel.yaml.round_trip_dump(mydict, f, indent=2)
,但它似乎不支持sort_keys
选项. ruamel.yaml
也似乎没有任何详尽的文档,然后搜索Google "ruamel.yaml排序"或"ruamel.yaml字母排序"没有达到我期望的简单程度.
but it doesn't seem to support the sort_keys
option. ruamel.yaml
also doesn't seem to have any exhaustive docs, and searching Google for "ruamel.yaml sort" or "ruamel.yaml alphabetize" didn't turn up anything at the level of simplicity I'd expect.
是否有一两个衬里用于漂亮地打印带有排序键的YAML文件?
Is there a one-or-two-liner for pretty-printing a YAML file with sorted keys?
(请注意,我需要递归地在整个容器中按字母顺序排列键;仅按字母顺序排列顶层是不够的.)
(Note that I need the keys to be alphabetized down through the whole container, recursively; just alphabetizing the top level is not good enough.)
请注意,如果我使用round_trip_dump
,则不会对键进行排序.如果使用safe_dump
,则输出不是YAML样式(或更重要的是Kubernetes样式).我不想在输出中使用[]
或{}
.
Notice that if I use round_trip_dump
, the keys are not sorted; and if I use safe_dump
, the output is not "YAML-style" (or more importantly "Kubernetes-style") YAML. I don't want []
or {}
in my output.
$ pip freeze | grep yaml
ruamel.yaml==0.12.5
$ python
>>> import ruamel.yaml
>>> mydict = {'a':1, 'b':[2,3,4], 'c':{'a':1,'b':2}}
>>> print ruamel.yaml.round_trip_dump(mydict) # right format, wrong sorting
a: 1
c:
a: 1
b: 2
b:
- 2
- 3
- 4
>>> print ruamel.yaml.safe_dump(mydict) # wrong format, right sorting
a: 1
b: [2, 3, 4]
c: {a: 1, b: 2}
推荐答案
此:
import sys
import ruamel.yaml
mydict = dict(a1=1, a2=2, a3=3, a11=11, a21=21)
ruamel.yaml.round_trip_dump(mydict, sys.stdout)
提供未排序的输出.在我的系统上:
gives non-sorted output. On my system:
a11: 11
a2: 2
a21: 21
a3: 3
a1: 1
通过添加:
my_sorted_dict = ruamel.yaml.comments.CommentedMap()
for k in sorted(mydict):
my_sorted_dict[k] = mydict[k]
ruamel.yaml.round_trip_dump(my_sorted_dict, sys.stdout)
这将被排序:
a1: 1
a11: 11
a2: 2
a21: 21
a3: 3
注释映射是ruamel.yaml
在进行往返(装入+转储)时使用的结构,往返旨在将密钥保持在以前的顺序.
The commented map is the structure ruamel.yaml
uses when doing a round-trip (load+dump) and round-tripping is designed to keep the keys in the order that they were before.
如果从YAML文件中加载了mydict
并且仅需要添加几个键,则还可以遍历mydict
的键并插入(mydict.insert(pos, new_key, new_value)
)键值对.
If you loaded mydict
from a YAML file and only need to add a few keys, you can also walk over mydict
's keys and insert (mydict.insert(pos, new_key, new_value)
) the key value pairs.
当然,如果不需要任何其他特殊功能,则可以通过执行普通的safe_dump()
来获得相同的输出:
Of course you can get the same output by doing a normal safe_dump()
if you don't need any of the other special features:
ruamel.yaml.safe_dump(mydict, sys.stdout, allow_unicode=True,
default_flow_style=False)
这篇关于ruamel.yaml是否相当于sort_keys?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!