PyYaml向后倾倒东西

Mat*_*bel 1 python yaml pyyaml

所以我有一个yaml文件,包含许多琐事问题和答案列表.但是,每当我尝试加载此文件并使用pyyaml将内容转储到python中时,它会向后转储它们.我不确定这是我的yaml文件还是我的库有问题.

假设我的一个问题/答案对在yaml文件中看起来像这样 -

{"question": "What is the name of this sequence of numbers: 1, 1, 2, 3, 5, 8, 13, ...", 
 "answer": ["The Fibonacci Sequence", "The Padovan Sequence", "The Morris Sequence"]}
Run Code Online (Sandbox Code Playgroud)

当我在那个python字典上使用yaml.dump()时,它转储了这个 -

answer: [fibonacci, padovan, morris]\nquestion: 'what sequence is this: 1, 1, 2, 3, 5, 8, 13, ...'\n"
Run Code Online (Sandbox Code Playgroud)

我在期待这个 -

- question: "What is the name of this sequence of numbers: 1, 1, 2, 3, 5, 8, 13, ..."
  answer: ["The Fibonacci Sequence", "The Padovan Sequence", "The Morris Sequence"]
Run Code Online (Sandbox Code Playgroud)

我在这里做错了吗?

cge*_*cge 6

我在这里有一个不同的答案.如果由于可读性以外的原因,元素的顺序对您很重要,那么dbaupp的答案是正确的.如果你想在回答前显示问题的唯一原因是为了使文件更易于阅读,那么你不需要使用!! omap,而是可以使用自定义代表来获得你想要的顺序.

首先,你在没有 - 前面的转储器转储的问题是因为你只是转储一个映射,而不是它们的列表.把你的词典放在一个列表中,这将被修复.所以我们从:

d = [{"question": "What is the name of this sequence of numbers: 1, 1, 2, 3, 5, 8, 13, ...", 
 "answer": ["The Fibonacci Sequence", "The Padovan Sequence", "The Morris Sequence"]}]
Run Code Online (Sandbox Code Playgroud)

现在我们有一个我们想要输出的特定顺序,所以我们将指定它,并使用该顺序转换为OrderedDict:

from collections import OrderedDict
order = ['question', 'answer']
do = [ OrderedDict( sorted( z.items(), key=lambda x: order.index(x[0]) ) ) for z in d ]
Run Code Online (Sandbox Code Playgroud)

接下来,我们需要做到这一点,以便PyYAML知道如何处理OrderedDict.在这种情况下,我们不希望它是一个!! omap,我们只想要一个具有特定顺序的映射.对于某些我不清楚的动机,如果你给dumper.represent_mapping一个dict,或者任何带有items属性的东西,它会在转储之前对项进行排序,但是如果你给它输出items()(例如,一个(key)列表,值)元组),它不会.因此我们可以使用

def order_rep(dumper, data):
    return dumper.represent_mapping( u'tag:yaml.org,2002:map', data.items(), flow_style=False )
yaml.add_representer( OrderedDict, order_rep )
Run Code Online (Sandbox Code Playgroud)

然后,我们的输出结果print yaml.dump(do)如下:

- question: 'What is the name of this sequence of numbers: 1, 1, 2, 3, 5, 8, 13, ...'
  answer: [The Fibonacci Sequence, The Padovan Sequence, The Morris Sequence]
Run Code Online (Sandbox Code Playgroud)

有许多不同的方法可以做到这一点.使用OrderedDict实际上根本不需要,你只需要问题/答案对就可以为某个类编写代表.

再一次,要意识到这只是为了人类的可读性和审美目的.这里的顺序不具有任何YAML意义,就像你使用!! omap一样.看起来这对您的可读性而言非常重要.