问题描述
我正在使用 yaml.dump
输出一个字典.它根据键按字母顺序打印出每个项目.
有没有办法控制键/值对的顺序?
在我的特定用例中,反向打印(巧合)就足够了.不过,为了完整起见,我正在寻找一个答案,显示如何更精确地控制订单.
我已经考虑过使用 collections.OrderedDict
但 PyYAML 不(似乎)支持它.我还研究了 yaml.Dumper
的子类化,但我无法弄清楚它是否具有更改项目顺序的能力.
可能有更好的解决方法,但我在文档或源代码中找不到任何内容.
Python 2(见评论)
我子类化了 OrderedDict
并让它返回一个不可排序的项目列表:
from collections import OrderedDict类 UnsortableList(list):def sort(self, *args, **kwargs):经过类 UnsortableOrderedDict(OrderedDict):def 项目(自我,*args,**kwargs):返回 UnsortableList(OrderedDict.items(self, *args, **kwargs))yaml.add_representer(UnsortableOrderedDict, yaml.representer.SafeRepresenter.represent_dict)
它似乎有效:
>>>d = UnsortableOrderedDict([... ('z', 0),... ('y', 0),... ('x', 0)...])>>>yaml.dump(d, default_flow_style=False)'z: 0y: 0x: 0'Python 3 或 2(见评论)
您也可以编写自定义表示器,但我不知道您稍后会不会遇到问题,因为我从中剥离了一些样式检查代码:
导入yaml从集合导入 OrderedDict定义代表_ordereddict(转储器,数据):价值 = []对于 data.items() 中的 item_key、item_value:node_key = dumper.represent_data(item_key)node_value = dumper.represent_data(item_value)value.append((node_key, node_value))返回 yaml.nodes.MappingNode(u'tag:yaml.org,2002:map', value)yaml.add_representer(OrderedDict,represent_ordereddict)
但是有了它,你可以使用原生的 OrderedDict
类.
I'm using yaml.dump
to output a dict. It prints out each item in alphabetical order based on the key.
>>> d = {"z":0,"y":0,"x":0}
>>> yaml.dump( d, default_flow_style=False )
'x: 0
y: 0
z: 0
'
Is there a way to control the order of the key/value pairs?
In my particular use case, printing in reverse would (coincidentally) be good enough. For completeness though, I'm looking for an answer that shows how to control the order more precisely.
I've looked at using collections.OrderedDict
but PyYAML doesn't (seem to) support it. I've also looked at subclassing yaml.Dumper
, but I haven't been able to figure out if it has the ability to change item order.
There's probably a better workaround, but I couldn't find anything in the documentation or the source.
Python 2 (see comments)
I subclassed OrderedDict
and made it return a list of unsortable items:
from collections import OrderedDict
class UnsortableList(list):
def sort(self, *args, **kwargs):
pass
class UnsortableOrderedDict(OrderedDict):
def items(self, *args, **kwargs):
return UnsortableList(OrderedDict.items(self, *args, **kwargs))
yaml.add_representer(UnsortableOrderedDict, yaml.representer.SafeRepresenter.represent_dict)
And it seems to work:
>>> d = UnsortableOrderedDict([
... ('z', 0),
... ('y', 0),
... ('x', 0)
... ])
>>> yaml.dump(d, default_flow_style=False)
'z: 0
y: 0
x: 0
'
Python 3 or 2 (see comments)
You can also write a custom representer, but I don't know if you'll run into problems later on, as I stripped out some style checking code from it:
import yaml
from collections import OrderedDict
def represent_ordereddict(dumper, data):
value = []
for item_key, item_value in data.items():
node_key = dumper.represent_data(item_key)
node_value = dumper.represent_data(item_value)
value.append((node_key, node_value))
return yaml.nodes.MappingNode(u'tag:yaml.org,2002:map', value)
yaml.add_representer(OrderedDict, represent_ordereddict)
But with that, you can use the native OrderedDict
class.
这篇关于PyYAML 可以按非字母顺序转储 dict 项目吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!