使用Python输出YAML:输入中没有列表的格式不正确

使用Python输出YAML:输入中没有列表的格式不正确,python,yaml,pyyaml,ruamel.yaml,Python,Yaml,Pyyaml,Ruamel.yaml,我正在尝试从Python字典创建YAML。到目前为止,我已经尝试了PyYAML和ruamel.yaml,它们都有相同的结果:如果输入字典不包含列表,则输出的格式不正确 以下是脚本: from ruamel import yaml import sys yaml.dump({'name': 'Enterprise', 'class': 'Galaxy', 'armament': ['photon torpedoes','phasers'], 'number': 1701}, sys.stdout

我正在尝试从Python字典创建YAML。到目前为止,我已经尝试了PyYAML和ruamel.yaml,它们都有相同的结果:如果输入字典不包含列表,则输出的格式不正确

以下是脚本:

from ruamel import yaml
import sys

yaml.dump({'name': 'Enterprise', 'class': 'Galaxy', 'armament': ['photon torpedoes','phasers'], 'number': 1701}, sys.stdout)
print('\n')
yaml.dump({'name': 'Enterprise', 'class': 'Galaxy', 'number': 1701}, sys.stdout)
以下是输出:

armament: [photon torpedoes, phasers]
class: Galaxy
name: Enterprise
number: 1701

{class: Galaxy, name: Enterprise, number: 1701}
所需的输出是,第二个YAML转储的格式应与第一个类似。这是怎么回事?

这在“如何进行一致输出”中有说明:

yaml.dump({'name': 'Enterprise', 'class': 'Galaxy', 'number': 1701},
  sys.stdout,
  default_flow_style=False) # <- the important parameter

class: Galaxy
name: Enterprise
number: 1701
yaml.dump({'name':'Enterprise','class':'Galaxy','number':1701},
sys.stdout,

默认_flow_style=False)#您使用的是旧式API,默认输出任何叶 流样式中的节点。在您的情况下,列表/顺序
[光子鱼雷,相位器]
和秒转储(根级别为 叶节点)


假设您不仅需要解释发生这种情况的原因,还需要了解如何使用新API,使用
ruamel.yaml.yaml
的实例来更改这种行为,那么默认设置是使所有内容都成为流样式,包括所有叶节点:

from ruamel.yaml import YAML
import sys

yaml = YAML()

yaml.dump({'name': 'Enterprise', 'class': 'Galaxy', 'armament': ['photon torpedoes','phasers'], 'number': 1701}, sys.stdout)
print()
yaml.dump({'name': 'Enterprise', 'class': 'Galaxy', 'number': 1701}, sys.stdout)
给予:

name: Enterprise
class: Galaxy
armament:
- photon torpedoes
- phasers
number: 1701

name: Enterprise
class: Galaxy
number: 1701
仍然不是你想要的:-)

现在有两个选项可以获得所指示的内容:第一个转储上的叶节点流样式和第二个转储上的叶节点块样式

第一个是将一个实例设置为
default\u flow\u style
用于第一次转储和 第二次转储的“正常”一次:

from ruamel.yaml import YAML
import sys

yaml = YAML()

yaml.default_flow_style = None
yaml.dump({'name': 'Enterprise', 'class': 'Galaxy', 'armament': ['photon torpedoes','phasers'], 'number': 1701}, sys.stdout)
print()
yaml = YAML()
yaml.dump({'name': 'Enterprise', 'class': 'Galaxy', 'number': 1701}, sys.stdout)
其中:

name: Enterprise
class: Galaxy
armament: [photon torpedoes, phasers]
number: 1701

name: Enterprise
class: Galaxy
number: 1701
第二个选项是显式设置对象的流样式 要作为序列输出的。因此,您必须创建一个
ruamel.yaml.comments.CommentedSeq
实例,通常使用 加载以保留流/块样式、注释等时:

from ruamel.yaml import YAML, comments
import sys

yaml = YAML()

armaments = comments.CommentedSeq(['photon torpedoes','phasers'])
armaments.fa.set_flow_style()
yaml.dump({'name': 'Enterprise', 'class': 'Galaxy', 'armament': armaments, 'number': 1701}, sys.stdout)
print()
yaml.dump({'name': 'Enterprise', 'class': 'Galaxy', 'number': 1701}, sys.stdout)
它还提供:

name: Enterprise
class: Galaxy
armament: [photon torpedoes, phasers]
number: 1701

name: Enterprise
class: Galaxy
number: 1701
当然,第二个选项为您提供了精细的控制(还有一个
CommentedMap
) 可以在数据层次结构的所有级别上拥有这些对象,而不仅仅是在作为集合的叶子上



请注意,从具有所需格式的YAML文件加载所需输出时,您不必经历任何这些滑稽动作。在这种情况下,dict resp。列表式实例是使用正确的流/块样式创建的,因此在更改/添加值并转储回时,输出不会意外更改。

您确定这是脚本的输出吗?
打印('\n')
实际上会在YAML转储之间放置两个换行符。