Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用python将字典写入YAML文件时出现的问题_Python_Python 3.x_Yaml_Ruamel.yaml_Openstack Heat - Fatal编程技术网

用python将字典写入YAML文件时出现的问题

用python将字典写入YAML文件时出现的问题,python,python-3.x,yaml,ruamel.yaml,openstack-heat,Python,Python 3.x,Yaml,Ruamel.yaml,Openstack Heat,我想将字典写入YAML文件,下面是我目前正在做的事情 from ruamel.yaml import YAML Flavor_Details = {'Flavor_Details':{ 'type': 'OS::Nova::Flavor', 'properties': { 'name': 'test-flavor',

我想将字典写入YAML文件,下面是我目前正在做的事情

from ruamel.yaml import YAML

Flavor_Details = {'Flavor_Details':{
                  'type': 'OS::Nova::Flavor', 
                  'properties': {
                                 'name': 'test-flavor', 
                                 'extra_specs': {"hw:cpu_policy": 'shared'}, 
                                 'ram': 4096, 
                                 'vcpus': 4, 
                                 'disk': 8
                                }
                 }}

output_section = {
                    'server_public_ip':{
                        'description': 'Floating IP address of server',
                        'value': { 'get_attr': [ 'server_floating_ip', 'ip' ] }
                    }
                }

resources_dict = {}
resources_dict.update(Flavor_Details)
resources_dict.update(output_section)

yaml = YAML(typ= 'safe')

with open('test.yaml', 'w') as outfile:
     yaml.dump(resources_dict,outfile)
这是YAML文件中的结果

Flavor_Details:
    properties:
      disk: 8
      extra_specs: {hw:cpu_policy: shared}
      name: test-flavor
      ram: 4096
      vcpus: 4
    type: OS::Nova::Flavor
server_public_ip:
    description: Floating IP address of server
    value:
      get_attr: [server__floating_ip, ip]
但我想要这样的结果:

Flavor_Details:
    properties:
      disk: 8
      extra_specs: {"hw:cpu_policy": shared}
      name: test-flavor
      ram: 4096
      vcpus: 4
    type: OS::Nova::Flavor
server_public_ip:
    description: Floating IP address of server
    value: {get_attr: [server__floating_ip, ip]}
我希望
“hw:cpu_policy”
作为一个字符串,因为
hw
cpu_policy
之间的
值中的
我希望像
{get_attr:[server\u floating\u ip,ip]}
这样


有什么办法可以得到这样的东西吗?

你可以得到你想要的东西,但不能用安全的垃圾桶。基础的基于C的代码不允许 对于所需的细粒度控件

您需要使用默认的(往返)转储程序,它允许这种类型的 详细控制,因为它需要它来尝试和保留往返的布局

我希望您了解YAML文件中关于
hw:cpu\u policy
的引号是 根据YAML规范,这不是必需的,但存在一些不正确的情况 YAML解析器的实现存在问题

import sys
import ruamel.yaml

def Q(s):
    return ruamel.yaml.scalarstring.DoubleQuotedScalarString(s)

def F(*args, **kw):
    x = ruamel.yaml.comments.CommentedMap()
    x.fa.set_flow_style()
    for a in args:
        x.update(a)
    x.update(kw)
    return x


Flavor_Details = {'Flavor_Details':{
                  'type': 'OS::Nova::Flavor', 
                  'properties': {
                                 'name': 'test-flavor', 
                                 'extra_specs': F({Q("hw:cpu_policy"): 'shared'}), 
                                 'ram': 4096, 
                                 'vcpus': 4, 
                                 'disk': 8
                                }
                 }}

output_section = {
                    'server_public_ip':{
                        'description': 'Floating IP address of server',
                        'value': F(get_attr=['server_floating_ip', 'ip'])
                    }
                }

resources_dict = {}
resources_dict.update(Flavor_Details)
resources_dict.update(output_section)

yaml = ruamel.yaml.YAML()
# yaml.indent(mapping=4, sequence=4, offset=2)
yaml.dump(resources_dict, sys.stdout)
其中:

Flavor_Details:
  type: OS::Nova::Flavor
  properties:
    name: test-flavor
    extra_specs: {"hw:cpu_policy": shared}
    ram: 4096
    vcpus: 4
    disk: 8
server_public_ip:
  description: Floating IP address of server
  value: {get_attr: [server_floating_ip, ip]}
Flavor_Details:
  properties:
    disk: 8
    extra_specs: {"hw:cpu_policy": shared}
    name: cloudpeak-test-flavor
    ram: 4096
    vcpus: 4
  type: OS::Nova::Flavor
server_public_ip:
  description: Floating IP address of server
  value: {get_attr: [server_floating_ip, ip]}

另一种方法是返回PyYAML并对输出字符串使用简单的替换规则。还应注意
额外规格
的所需条目

import yaml as yl

Flavor_Details = {'Flavor_Details': {
                  'type': 'OS::Nova::Flavor',
                  'properties': {
                      'name': 'cloudpeak-test-flavor',
                      'extra_specs': "{'hw:cpu_policy': shared}",
                      'ram': 4096,
                      'vcpus': 4,
                      'disk': 8
                  }
                  }}

output_section = {
    'server_public_ip': {
        'description': 'Floating IP address of server',
        'value': '{get_attr: [server_floating_ip, ip]}'
    }
}

resources_dict = {}
resources_dict.update(Flavor_Details)
resources_dict.update(output_section)

out_str = yl.safe_dump(resources_dict)
out_str = out_str.replace("''", '"')
out_str = out_str.replace("'", '')

print(out_str)

with open('test-yl.yaml', 'w') as outfile:
    outfile.write(out_str)
其中:

Flavor_Details:
  type: OS::Nova::Flavor
  properties:
    name: test-flavor
    extra_specs: {"hw:cpu_policy": shared}
    ram: 4096
    vcpus: 4
    disk: 8
server_public_ip:
  description: Floating IP address of server
  value: {get_attr: [server_floating_ip, ip]}
Flavor_Details:
  properties:
    disk: 8
    extra_specs: {"hw:cpu_policy": shared}
    name: cloudpeak-test-flavor
    ram: 4096
    vcpus: 4
  type: OS::Nova::Flavor
server_public_ip:
  description: Floating IP address of server
  value: {get_attr: [server_floating_ip, ip]}

你有没有试着转义它
“{\\\“foo\\”:\\“bar\\”}”
?那是行不通的。带引号的字符串可以用YAML表示,但由于引号本身具有特殊的含义(至少在标量的开头),您将得到一个带引号的单标量,其中嵌入了双引号。无法实现映射的不一致缩进(根级别有4个空格,下面有2个空格),但是,如果您愿意,您可以使用注释掉的line@DirtyBit我希望如此,我对ruamel.yaml包相当熟悉