我有一个字典,我正在使用Python的yaml
模块将字典转换为Yaml。但是Yaml转换不正确。
output_data = {
'resources': [{
'type': 'compute.v1.instance',
'name': 'vm-created-by-deployment-manager',
'properties': {
'disks': [{
'deviceName': '$disks_deviceName$',
'boot': '$disks_boot$',
'initializeParams': {
'sourceImage': '$disks_initializeParams_sourceImage$'
},
'autoDelete': '$disks_autoDelete$',
'type': '$disks_type$'
}],
'machineType': '$machineType$',
'zone': '$zone$',
'networkInterfaces': [{
'network': '$networkInterfaces_network$'
}]
}
}]
}
我尝试了:
import yaml
f = open('meta.yaml', 'w+')
yaml.dump(output_data, f, allow_unicode=True)
以下是我得到的meta.yaml
文件:
resources:
- name: vm-created-by-deployment-manager
properties:
disks:
- autoDelete: $disks_autoDelete$
boot: $disks_boot$
deviceName: $disks_deviceName$
initializeParams: {sourceImage: $disks_initializeParams_sourceImage$}
type: $disks_type$
machineType: $machineType$
networkInterfaces:
- {network: $networkInterfaces_network$}
zone: $zone$
type: compute.v1.instance
在这里,{sourceImage: $disks_initializeParams_sourceImage$}
和{network: $networkInterfaces_network$}
就像是一个字典
。 这意味着内部字典内容不会转换为YAML格式。
我也尝试过,
output_data = eval(json.dumps(output_data))
ff = open('meta.yaml', 'w+')
yaml.dump(output_data, ff, allow_unicode=True)
但获取相同的yaml
文件内容。
我如何在Python中将完整的字典转换为yaml?
yaml.dump(output_data, ff, allow_unicode=True, default_flow_style=False)
- floydya