pyyaml输出不正确 [英] improper output with pyyaml

查看:111
本文介绍了pyyaml输出不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个YAML文件,test.yaml:

I have a YAML file, test.yaml:

test:
  server_group_1:
    type: OS::Nova::ServerGroup
    properties:
      name: { get_param: [server_groups, 5] }
      policies: [ { get_param: [server_group_types, 5] } ]

当我使用PyYAML读取和打印输出时,它在输出下方显示了与输入不同的内容

and when I use PyYAML to read and print the output it gives me below output, which is different from the input

test:
  server_group_1:
    properties:
      name:
        get_param:
        - server_groups
        - 5
      policies:
      - get_param:
        - server_group_types
        - 5
    type: OS::Nova::ServerGroup

代码:

import yaml
print yaml.dump(yaml.load(open('/test.yaml')), default_flow_style=False)

我希望输出与输入相同,这里的顺序也被更改

I want the output same as the input, here order is also getting changed

推荐答案

您的PyYAML短代码有几个问题,与映射中的键顺序(PyYAML始终排序)无关.

Your short PyYAML code has several problems, not related to the order of keys in mappings (which PyYAML always sorts).

几乎没有需要使用不带Loader参数的PyYAML的load(),据证明这是不安全的.而且,您无法为dump()提供流参数,这会导致将输出写入内存,被检索,然后您将其print.如果您想尝试PyYAML,应该只使用dump(yaml.safe_load(open('/test.yaml')), sys.stdout, default_flow_style=False).

There is almost never need to use PyYAML's load() without Loader parameter, which is documented to be unsafe. And you fail to provide a stream parameter to dump(), which causes the output to be written to memory, retrieved and then you print it. You should just use dump(yaml.safe_load(open('/test.yaml')), sys.stdout, default_flow_style=False) if you want to experiment with PyYAML.

尽管可以将PyYAML映射作为Python排序字​​典加载,但这并不简单.

Although it is possible to have PyYAML mapping loaded as Python ordered dictionaries, this is not trivial.

更麻烦的是嵌套流样式,例如{get_param: [server_groups, 5]},其中流样式列表嵌套在流样式映射中.用于流样式的PyYAML控件是以下三种之一:根本没有流样式,或者什么都没有流样式,或者所有叶节点都没有流样式.

Far more problematic is the nested flow style, e.g. {get_param: [server_groups, 5]} where a flow style list is nested in a flow style mapping. PyYAML control for flow style is one of three: no flow style at all, or everything flow-style, or all leaf nodes flow style.

您应该查看ruamel.yaml(免责声明:我是该软件包的作者),您可以在其中进行以下操作:

You should look at ruamel.yaml (disclaimer: I am the author of that package) where you can do:

import sys
import ruamel.yaml

yaml = ruamel.yaml.YAML()

with open('test.yaml') as fp:
    data = yaml.load(fp)
yaml.dump(data, sys.stdout)

这将保留关键顺序,并且流样式将在所有级别上保留.您唯一无法控制的是流样式花括号前后的多余空格,以及流样式方括号前后的多余空格.

this preserves the key order, and the flow style is preserved at all the levels. The only thing you cannot control is the extra spaces after/before the flow style curly braces and the inconsistent extra spaces after/before the flow style square brackets.

使用上面的代码,您将获得:

With the code above you get:

test:
  server_group_1:
    type: OS::Nova::ServerGroup
    properties:
      name: {get_param: [server_groups, 5]}
      policies: [{get_param: [server_group_types, 5]}]

这篇关于pyyaml输出不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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