在 Python 中控制 Yaml 序列化顺序 [英] Controlling Yaml Serialization Order in Python

查看:49
本文介绍了在 Python 中控制 Yaml 序列化顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在序列化 Python 字典时,如何控制 PyYaml 输出键/值对的顺序?

我在 Python 脚本中使用 Yaml 作为简单的序列化格式.我的 Yaml 序列化对象代表一种文档",因此为了最大程度地方便用户,我希望我的对象的名称"字段首先出现在文件中.当然,由于我的对象的 __getstate__ 返回的值是一个字典,而 Python 字典是无序的,name"字段将被序列化到输出中的随机位置.

例如

<预><代码>>>>导入 yaml>>>类文档(对象):... def __init__(self, name):... self.name = 姓名... self.otherstuff = '等等'... def __getstate__(self):...返回 self.__dict__.copy()...>>>doc = Document('obj-20111227')>>>打印 yaml.dump(doc, indent=4)!!python/对象:__main__.Document其他:等等名称:obj-20111227

解决方案

我花了几个小时挖掘 PyYAML 文档和票证,但我最终发现了 此评论 列出了一些用于将 OrderedDict 序列化为普通 YAML 映射(但保持顺序)的概念验证代码.

例如应用于我的原始代码,解决方案如下所示:

<预><代码>>>>导入 yaml>>>从集合导入 OrderedDict>>>def dump_anydict_as_map(anydict):... yaml.add_representer(anydict, _represent_dictorder)...>>>def _represent_dictorder( self, data):...如果 isinstance(data, Document):... return self.represent_mapping('tag:yaml.org,2002:map', data.__getstate__().items())... 别的:... return self.represent_mapping('tag:yaml.org,2002:map', data.items())...>>>类文档(对象):... def __init__(self, name):... self.name = 姓名... self.otherstuff = '等等'... def __getstate__(self):... d = OrderedDict()... d['name'] = self.name... d['otherstuff'] = self.otherstuff...返回d...>>>dump_anydict_as_map(文档)>>>doc = Document('obj-20111227')>>>打印 yaml.dump(doc, indent=4)!!python/对象:__main__.Document名称:obj-20111227其他:等等

How do you control how the order in which PyYaml outputs key/value pairs when serializing a Python dictionary?

I'm using Yaml as a simple serialization format in a Python script. My Yaml serialized objects represent a sort of "document", so for maximum user-friendliness, I'd like my object's "name" field to appear first in the file. Of course, since the value returned by my object's __getstate__ is a dictionary, and Python dictionaries are unordered, the "name" field will be serialized to a random location in the output.

e.g.

>>> import yaml
>>> class Document(object):
...     def __init__(self, name):
...         self.name = name
...         self.otherstuff = 'blah'
...     def __getstate__(self):
...         return self.__dict__.copy()
... 
>>> doc = Document('obj-20111227')
>>> print yaml.dump(doc, indent=4)
!!python/object:__main__.Document
otherstuff: blah
name: obj-20111227

解决方案

Took me a few hours of digging through PyYAML docs and tickets, but I eventually discovered this comment that lays out some proof-of-concept code for serializing an OrderedDict as a normal YAML map (but maintaining the order).

e.g. applied to my original code, the solution looks something like:

>>> import yaml
>>> from collections import OrderedDict
>>> def dump_anydict_as_map(anydict):
...     yaml.add_representer(anydict, _represent_dictorder)
... 
>>> def _represent_dictorder( self, data):
...     if isinstance(data, Document):
...         return self.represent_mapping('tag:yaml.org,2002:map', data.__getstate__().items())
...     else:
...         return self.represent_mapping('tag:yaml.org,2002:map', data.items())
... 
>>> class Document(object):
...     def __init__(self, name):
...         self.name = name
...         self.otherstuff = 'blah'
...     def __getstate__(self):
...         d = OrderedDict()
...         d['name'] = self.name
...         d['otherstuff'] = self.otherstuff
...         return d
... 
>>> dump_anydict_as_map(Document)
>>> doc = Document('obj-20111227')
>>> print yaml.dump(doc, indent=4)
!!python/object:__main__.Document
name: obj-20111227
otherstuff: blah

这篇关于在 Python 中控制 Yaml 序列化顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆