我有一个字典,我正在用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$'
I tried :
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
Here, {sourceImage: $disks_initializeParams_sourceImage$}
and {network: $networkInterfaces_network$}
is getting like dictionary
.这意味着内部
字典内容没有转换为yaml.
我也试过。
output_data = eval(json.dumps(output_data))
ff = open('meta.yaml', 'w+')
yaml.dump(output_data, ff, allow_unicode=True)
但得到相同的yaml
文件内容。
如何在Python中把完整的dictinary转换为yaml?