合并 python 中的两个 yaml 文件
merge two yaml files in python
我有两个 yaml 文件,如下所述
test1.yaml
resources:
server_group_1:
type: OS::Nova::ServerGroup
properties:
name: { get_param: [server_groups, 5] }
policies: [ { get_param: [server_group_types, 5] } ]
server_group_2:
type: OS::Nova::ServerGroup
properties:
name: { get_param: [server_groups, 8] }
policies: [ { get_param: [server_group_types, 8] } ]
output:
check_1:
description: Name of the instance
value: { get_attr: [check_1, vname] }
test2.yaml
resources:
server_group_4:
type: OS::Nova::ServerGroup
properties:
name: { get_param: [server_groups, 4] }
policies: [ { get_param: [server_group_types, 4] } ]
server_group_9:
type: OS::Nova::ServerGroup
properties:
name: { get_param: [server_groups, 7] }
policies: [ { get_param: [server_group_types, 7] } ]
output:
check_6:
description: Name of the instance
value: { get_attr: [check_6, vname] }
我想合并这两个文件并创建一个新的输出文件,所以我使用 pyyaml 顺序正在更改,在此
有人可以帮忙合并这些文件而不改变顺序吗?
最终的 yaml 应该是这样的
final.yaml
resources:
server_group_1:
type: OS::Nova::ServerGroup
properties:
name: { get_param: [server_groups, 5] }
policies: [ { get_param: [server_group_types, 5] } ]
server_group_2:
type: OS::Nova::ServerGroup
properties:
name: { get_param: [server_groups, 8] }
policies: [ { get_param: [server_group_types, 8] } ]
server_group_4:
type: OS::Nova::ServerGroup
properties:
name: { get_param: [server_groups, 4] }
policies: [ { get_param: [server_group_types, 4] } ]
server_group_9:
type: OS::Nova::ServerGroup
properties:
name: { get_param: [server_groups, 7] }
policies: [ { get_param: [server_group_types, 7] } ]
output:
check_1:
description: Name of the instance
value: { get_attr: [check_1, vname] }
check_6:
description: Name of the instance
value: { get_attr: [check_6, vname] }
已更新
我可以使用 ruamel.yaml 合并文件...这里是更新资源的示例代码
代码:
import ruamel.yaml
yaml = ruamel.yaml.YAML()
#Load the yaml files
with open('/test1.yaml') as fp:
data = yaml.load(fp)
with open('/test2.yaml') as fp:
data1 = yaml.load(fp)
#Add the resources from test2.yaml to test1.yaml resources
for i in data1['resources']:
print i,data1['resources'][i]
data['resources'].update({i:data1['resources'][i]})
#create a new file with merged yaml
yaml.dump(data,file('/tmp/lal.yaml', 'w'))
下面的示例代码很适合我合并两个 yaml 文件
import ruamel.yaml
yaml = ruamel.yaml.YAML()
#Load the yaml files
with open('/test1.yaml') as fp:
data = yaml.load(fp)
with open('/test2.yaml') as fp:
data1 = yaml.load(fp)
#Add the resources from test2.yaml to test1.yaml resources
for i in data1['resources']:
print i,data1['resources'][i]
data['resources'].update({i:data1['resources'][i]})
#create a new file with merged yaml
yaml.dump(data,file('/tmp/lal.yaml', 'w'))
另一种解决方案是使用 HiYaPyCo (https://pypi.org/project/HiYaPyCo/),它实现了保留元素顺序的合并。
import hiyapyco
yaml1 = """resources:
server_group_1:
type: OS::Nova::ServerGroup
properties:
name: { get_param: [server_groups, 5] }
policies: [ { get_param: [server_group_types, 5] } ]
server_group_2:
type: OS::Nova::ServerGroup
properties:
name: { get_param: [server_groups, 8] }
policies: [ { get_param: [server_group_types, 8] } ]
output:
check_1:
description: Name of the instance
value: { get_attr: [check_1, vname] }"""
yaml2 = """resources:
server_group_4:
type: OS::Nova::ServerGroup
properties:
name: { get_param: [server_groups, 4] }
policies: [ { get_param: [server_group_types, 4] } ]
server_group_9:
type: OS::Nova::ServerGroup
properties:
name: { get_param: [server_groups, 7] }
policies: [ { get_param: [server_group_types, 7] } ]
output:
check_6:
description: Name of the instance
value: { get_attr: [check_6, vname] }"""
merged_yaml = hiyapyco.load([yaml1, yaml2], method=hiyapyco.METHOD_MERGE)
print(hiyapyco.dump(merged_yaml))
输出:
resources:
server_group_1:
type: OS::Nova::ServerGroup
properties:
name:
get_param: [server_groups, 5]
policies:
- get_param: [server_group_types, 5]
server_group_2:
type: OS::Nova::ServerGroup
properties:
name:
get_param: [server_groups, 8]
policies:
- get_param: [server_group_types, 8]
server_group_4:
type: OS::Nova::ServerGroup
properties:
name:
get_param: [server_groups, 4]
policies:
- get_param: [server_group_types, 4]
server_group_9:
type: OS::Nova::ServerGroup
properties:
name:
get_param: [server_groups, 7]
policies:
- get_param: [server_group_types, 7]
output:
check_1:
description: Name of the instance
value:
get_attr: [check_1, vname]
check_6:
description: Name of the instance
value:
get_attr: [check_6, vname]
我有两个 yaml 文件,如下所述
test1.yaml
resources:
server_group_1:
type: OS::Nova::ServerGroup
properties:
name: { get_param: [server_groups, 5] }
policies: [ { get_param: [server_group_types, 5] } ]
server_group_2:
type: OS::Nova::ServerGroup
properties:
name: { get_param: [server_groups, 8] }
policies: [ { get_param: [server_group_types, 8] } ]
output:
check_1:
description: Name of the instance
value: { get_attr: [check_1, vname] }
test2.yaml
resources:
server_group_4:
type: OS::Nova::ServerGroup
properties:
name: { get_param: [server_groups, 4] }
policies: [ { get_param: [server_group_types, 4] } ]
server_group_9:
type: OS::Nova::ServerGroup
properties:
name: { get_param: [server_groups, 7] }
policies: [ { get_param: [server_group_types, 7] } ]
output:
check_6:
description: Name of the instance
value: { get_attr: [check_6, vname] }
我想合并这两个文件并创建一个新的输出文件,所以我使用 pyyaml 顺序正在更改,在此
有人可以帮忙合并这些文件而不改变顺序吗? 最终的 yaml 应该是这样的
final.yaml
resources:
server_group_1:
type: OS::Nova::ServerGroup
properties:
name: { get_param: [server_groups, 5] }
policies: [ { get_param: [server_group_types, 5] } ]
server_group_2:
type: OS::Nova::ServerGroup
properties:
name: { get_param: [server_groups, 8] }
policies: [ { get_param: [server_group_types, 8] } ]
server_group_4:
type: OS::Nova::ServerGroup
properties:
name: { get_param: [server_groups, 4] }
policies: [ { get_param: [server_group_types, 4] } ]
server_group_9:
type: OS::Nova::ServerGroup
properties:
name: { get_param: [server_groups, 7] }
policies: [ { get_param: [server_group_types, 7] } ]
output:
check_1:
description: Name of the instance
value: { get_attr: [check_1, vname] }
check_6:
description: Name of the instance
value: { get_attr: [check_6, vname] }
已更新
我可以使用 ruamel.yaml 合并文件...这里是更新资源的示例代码
代码:
import ruamel.yaml
yaml = ruamel.yaml.YAML()
#Load the yaml files
with open('/test1.yaml') as fp:
data = yaml.load(fp)
with open('/test2.yaml') as fp:
data1 = yaml.load(fp)
#Add the resources from test2.yaml to test1.yaml resources
for i in data1['resources']:
print i,data1['resources'][i]
data['resources'].update({i:data1['resources'][i]})
#create a new file with merged yaml
yaml.dump(data,file('/tmp/lal.yaml', 'w'))
下面的示例代码很适合我合并两个 yaml 文件
import ruamel.yaml
yaml = ruamel.yaml.YAML()
#Load the yaml files
with open('/test1.yaml') as fp:
data = yaml.load(fp)
with open('/test2.yaml') as fp:
data1 = yaml.load(fp)
#Add the resources from test2.yaml to test1.yaml resources
for i in data1['resources']:
print i,data1['resources'][i]
data['resources'].update({i:data1['resources'][i]})
#create a new file with merged yaml
yaml.dump(data,file('/tmp/lal.yaml', 'w'))
另一种解决方案是使用 HiYaPyCo (https://pypi.org/project/HiYaPyCo/),它实现了保留元素顺序的合并。
import hiyapyco
yaml1 = """resources:
server_group_1:
type: OS::Nova::ServerGroup
properties:
name: { get_param: [server_groups, 5] }
policies: [ { get_param: [server_group_types, 5] } ]
server_group_2:
type: OS::Nova::ServerGroup
properties:
name: { get_param: [server_groups, 8] }
policies: [ { get_param: [server_group_types, 8] } ]
output:
check_1:
description: Name of the instance
value: { get_attr: [check_1, vname] }"""
yaml2 = """resources:
server_group_4:
type: OS::Nova::ServerGroup
properties:
name: { get_param: [server_groups, 4] }
policies: [ { get_param: [server_group_types, 4] } ]
server_group_9:
type: OS::Nova::ServerGroup
properties:
name: { get_param: [server_groups, 7] }
policies: [ { get_param: [server_group_types, 7] } ]
output:
check_6:
description: Name of the instance
value: { get_attr: [check_6, vname] }"""
merged_yaml = hiyapyco.load([yaml1, yaml2], method=hiyapyco.METHOD_MERGE)
print(hiyapyco.dump(merged_yaml))
输出:
resources:
server_group_1:
type: OS::Nova::ServerGroup
properties:
name:
get_param: [server_groups, 5]
policies:
- get_param: [server_group_types, 5]
server_group_2:
type: OS::Nova::ServerGroup
properties:
name:
get_param: [server_groups, 8]
policies:
- get_param: [server_group_types, 8]
server_group_4:
type: OS::Nova::ServerGroup
properties:
name:
get_param: [server_groups, 4]
policies:
- get_param: [server_group_types, 4]
server_group_9:
type: OS::Nova::ServerGroup
properties:
name:
get_param: [server_groups, 7]
policies:
- get_param: [server_group_types, 7]
output:
check_1:
description: Name of the instance
value:
get_attr: [check_1, vname]
check_6:
description: Name of the instance
value:
get_attr: [check_6, vname]