使用PyYam将Python字典放入yaml文档

2024-04-29 09:59:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我有两个python字典,要将它们写入一个yaml文件,其中包含两个文档:

definitions = {"one" : 1, "two" : 2, "three" : 3}
actions = {"run" : "yes", "print" : "no", "report" : "maybe"}

yaml文件应该如下所示:

--- !define
one: 1
two: 2
three: 3

-- !action
run: yes
print: no
report: maybe
...

使用PyYaml,我没有找到一个明确的方法来做到这一点。我相信有一个简单的方法,但是深入研究PyYaml文档,只会让我感到困惑。我需要一个翻车机,发射器,还是什么?每种类型的输出是什么类型的?山药短信?山药节点?YAMLObject公司?不管怎样,我很感谢你的澄清。


以下是联合国大学的回答,这是我能想到的最简洁的版本:

DeriveYAMLObjectWithTag是一个函数,用于创建一个新类,该类派生自具有所需标记的YAMLObject:

def DeriveYAMLObjectWithTag(tag):
    def init_DeriveYAMLObjectWithTag(self, **kwargs):
        """ __init__ for the new class """
        self.__dict__.update(kwargs)

    new_class = type('YAMLObjectWithTag_'+tag,
                    (yaml.YAMLObject,),
                    {'yaml_tag' : '!{n}'.format(n = tag),
                    '__init__' :  init_DeriveYAMLObjectWithTag})
    return new_class

下面是如何使用DeriveYAMLObjectWithTag获取所需的Yaml:

definitions = {"one" : 1, "two" : 2, "three" : 3, "four" : 4}
actions = {"run" : "yes", "print" : "no", "report" : "maybe"}
namespace = [DeriveYAMLObjectWithTag('define')(**definitions),
             DeriveYAMLObjectWithTag('action')(**actions)]

text = yaml.dump_all(namespace,
                     default_flow_style = False,
                     explicit_start = True)

感谢所有的回答者。我觉得PyYaml中缺少功能,这是克服它的最优雅的方法。


Tags: norunreportactionsyamlinittagone
2条回答

怎么样:

class Bunch(yaml.YAMLObject):
    yaml_tag = u'!Bunch'
    def __init__(self, **kwargs):
        self.__dict__.update(kwargs)
    def __repr__(self):
        return '{c}({a})'.format(
            c = self.__class__.__name__,
            a = ', '.join(
                ['='.join(map(str,item)) for item in self.__dict__.items()]))
tag_names = ['define', 'action']
namespace = {}
for name in tag_names:
    namespace[name] = type(name, (Bunch,), {'yaml_tag':u'!{n}'.format(n = name)})

definitions = {"one" : 1, "two" : 2, "three" : 3}
actions = {"run" : "yes", "print" : "no", "report" : "maybe"}
text = yaml.dump_all([namespace['define'](**definitions),
                      namespace['action'](**actions)],
                     default_flow_style = False,
                     explicit_start = True)
print(text)

会产生

--- !define
one: 1
three: 3
two: 2
--- !action
print: 'no'
report: maybe
run: 'yes'

并将YAML加载回Python对象中:

for item in  yaml.load_all(text):
    print(item)
    # define(one=1, three=3, two=2)
    # action(print=no, report=maybe, run=yes)

YAMLObject的子类用于创建application-specific tags.

好吧,我还在研究自动评论(现在找不到相关文档),但这应该可以做到:

import yaml

definitions = {"one" : 1, "two" : 2, "three" : 3}
actions = {"run" : "yes", "print" : "no", "report" : "maybe"}

output = yaml.dump(actions, default_flow_style=False, explicit_start=True)
output += yaml.dump(definitions, default_flow_style=False, explicit_start=True)

print output

请注意,字典是无序的,因此无法保证生成的YAML的顺序。如果你想在家里点菜-看看OrderedDict

相关问题 更多 >