如何使用python(非字母顺序)保留YAML文件中键值的顺序

时间:2018-02-21 18:12:12

标签: python python-2.7 yaml pyyaml

我有下面的python代码用于生成(转储)YAML文档到文件。

import yaml
import os
device_name = "NN41_R11"
ip = "10.110.11.11"
port = "2022"
def genyam():
    data  = {
         "testbed" : {
            "name"  : "boot_ios"},

        "devices"  :  {
            device_name  :  {
                "type"  : "IOS",
                "connections"  : {
                    "defaults" : {
                        "class"  :  "con.con",
                    "a"  :  {
                        "protocol" : "telnet",
                        "ip" : ip,
                        "port" : port,
                    }
                    }
                }
            }
        }
        }

    with open('/tmp/testbed.yaml', 'w') as outfile:
        yaml.dump(data, outfile, default_flow_style=False)`

生成以下YAML文件

devices:
  NN41_R11:
    connections:
      defaults:
        a:
          ip: 10.110.11.11
          port: '2022'
          protocol: telnet
        class: con.con
    type: IOS
testbed:
  name: boot_ios

虽然键值缩进是正确的,但它没有以正确的顺序生成。我想先测试一下&然后设备现在却相反。我怀疑它是按字母顺序倾倒的。 NN41_R11又是一个字典,其中包含type & connectionstype& connections在同一级别生成但需要先type:IOS并在connections之下)。寻找有序转储基本

生成的YAML文档应如下所示:

testbed:
    name: "boot-ios"
devices:
    NN41_R11:
        type: IOS
        connections:
            defaults:
                 class: 'con.con'
            a:
              protocol: telnet
              ip: 10.110.11.11
              port: 2022

1 个答案:

答案 0 :(得分:0)

我建议你看一下ruamel.yaml(免责声明:我是该软件包的作者),它专门用于在加载和转储(即往返)YAML文档时保留密钥的顺序,也可以很容易地使用用动态生成YAML文档。

您必须以某种方式在源代码中对键值对进行排序,因为尽管Python源中存在顺序,但dict中的名称为data并未保留。 omap类型(即ruamel.yaml.comments.CommentedMap)可以使用元组列表进行初始化,但我经常发现使用逐步分配更容易。

要围绕那些不需要它的字符串获取双引号和单引号,请使用dq(即ruamel.yaml.scalarstring.DoubleQuotedScalarString)resp。 sq(即ruamel.yaml.scalarstring.SingleQuotedScalarString

您可以通过将其指定为int来删除端口周围的引号。

import sys
import ruamel.yaml
from ruamel.yaml.comments import CommentedMap as omap
from ruamel.yaml.scalarstring import DoubleQuotedScalarString as dq
from ruamel.yaml.scalarstring import SingleQuotedScalarString as sq

yaml = ruamel.yaml.YAML()
yaml.indent(mapping=4)

device_name = "NN41_R11"
ip = "10.110.11.11"
port = 2022

def genyam():
    # initialise omap with list of tuples that are key-value-pairs
    data = omap([
        ('testbed', omap([('name', dq('boot_ios'))])),
    ])
    # or add in the order you want them in the YAML document
    data['devices'] = devices = omap()
    devices[device_name] = name = omap()
    name['type'] = 'IOS'
    name['connections'] = connections = omap()
    connections['defaults'] = omap([('class', sq('con.con')),])
    connections['a'] = a = omap()
    a['protocol'] = 'telnet'
    a['ip'] = ip
    a['port'] = port
    yaml.dump(data, sys.stdout)


genyam()

给出:

testbed:
    name: "boot_ios"
devices:
    NN41_R11:
        type: IOS
        connections:
            defaults:
                class: 'con.con'
            a:
                protocol: telnet
                ip: 10.110.11.11
                port: 2022

ruamel.yaml中没有办法(在PyYAML中甚至更少)在输出中得到不同映射的不同缩进(你大多有四个,但也有五个和两个位置缩进)。 / p>

一种完全不同的方法是为您的YAML制作模板,并加载和转储以确保它是有效的YAML(填写模板后):

import sys
import ruamel.yaml

yaml_str = """\
testbed:
    name: "boot_ios"
devices:
    {device_name}:
        type: IOS
        connections:
            defaults:
                class: 'con.con'
            a:
                protocol: telnet
                ip: {ip}
                port: {port}
"""

yaml = ruamel.yaml.YAML()
yaml.indent(mapping=4)
yaml.preserve_quotes = True

def genyam2(device_name, ip, port):
    data = yaml.load(yaml_str.format(device_name=device_name, ip=ip, port=port))
    yaml.dump(data, sys.stdout)

genyam2(device_name = "NN41_R11", ip = "10.110.11.11", port = 2022)

这与前一个示例具有相同的输出,因为保留了往返顺序(如果指定yaml.preseve_quotes = True,也会保留多余的引号)