我有一个用例,我需要从新的YAML文件中选择key:value对。检查旧的YAML文件中是否存在该密钥,以及是否复制了该值并将其设置在新的YAML文件中 此外,如果该密钥在旧文件中不存在,则询问用户。
代码:
copyfile('all.isv', '/home/ubuntu/tmp/deploy/all')
with open("/home/ubuntu/ansible-environments/aws/lp/all", 'r') as f1:
try:
oldvars = yaml.load(f1)
with open("/home/ubuntu/tmp/deploy/all", 'rw') as f2:
newvars = yaml.load(f2)
for key,value in newvars.items():
print key, ":", value
if key in f1:
value = oldvars.items(value)
print key,value
f2.write(value)
else:
value = raw_input("Enter the value ")
它不起作用。无法理解如何检查旧文件中的密钥并在新文件中为该密钥写入值。
新文件:
# Enter the release and build you wish to deploy
release: "4.0"
build: "4_0_178"
ems_release: "4.0"
ems_build: "4_0_982"
build_type: "gold_master"
# The name prefix is synonymous with the stack (i.e. pdx02-cloud-prod)
name_prefix: syd01-devops-test
# The deployment type is one of: [ test | trial | dev | prod ]
deployment_type: test
# deployment_url is typically the same value as deployment type unless it is a premium deployment.
# In that case deployment_type is set to prod and deployment_url is either dev, trial or test
deployment_url: "{{ deployment_type }}"
some_new_var: hello
旧文件:
# Enter the release and build you wish to deploy
release: "4.0"
build: "4_0_178"
ems_release: "4.0"
ems_build: "4_0_999"
build_type: test_build
# The name prefix is synonymous with the stack (i.e. pdx02-cloud-prod)
name_prefix: syd01-devops-deepali
# The deployment type is one of: [ test | trial | dev | prod ]
deployment_type: trial
# deployment_url is typically the same value as deployment type unless it is a premium deployment.
# In that case deployment_type is set to prod and deployment_url is either dev, trial or test
deployment_url: "{{ deployment_type }}"
预期:使用两个文件(旧的和新的)生成文件
# Enter the release and build you wish to deploy
release: "4.0"
build: "4_0_178"
ems_release: "4.0"
ems_build: "4_0_999"
build_type: test_build
# The name prefix is synonymous with the stack (i.e. pdx02-cloud-prod)
name_prefix: syd01-devops-deepali
# The deployment type is one of: [ test | trial | dev | prod ]
deployment_type: trial
# deployment_url is typically the same value as deployment type unless it is a premium deployment.
# In that case deployment_type is set to prod and deployment_url is either dev, trial or test
deployment_url: "{{ deployment_type }}"
some_new_var: Value provided by user as input
答案 0 :(得分:1)
这是无效的Python,您的try
没有匹配的except
。除此之外,没有必要在用于阅读" old"中的with语句的上下文中打开第二个文件。文件。因此,请从:
import ruamel.yaml
copyfile('all.isv', '/home/ubuntu/tmp/deploy/all')
with open("/home/ubuntu/ansible-environments/aws/lp/all", 'r') as f1:
oldvars = ruamel.yaml.round_trip_load(f1)
你不能打开一个YAML文件进行阅读和写作,因此只需阅读它(用于读取和写入文件的开放是用' r +'不用' rw&#39 ):
with open("/home/ubuntu/tmp/deploy/all", 'r') as f2:
newvars = ruamel.yaml.round_trip_load(f2)
之后继续取消缩进并在适当时更新来自oldvars的newvars:
for key in newvars:
if key in oldvars:
# this copies the value from the old file if the key exists in there
value = oldvars[key]
else:
# ask the user for a new value
value = raw_input("Enter the value ")
# update the value in newvars
newvars[key] = value
# and write the update mapping back
with open("/home/ubuntu/tmp/deploy/all", 'w') as f2:
ruamel.yaml.round_trip_dump(newvars, f2)
合并并命名您的文件old.yaml
和new.yaml
并使用' abcd
'来回复提示:
import sys
import ruamel.yaml
with open('new.yaml') as fp:
newvars = ruamel.yaml.round_trip_load(fp)
with open('old.yaml') as fp:
oldvars = ruamel.yaml.round_trip_load(fp)
for key in newvars:
if key in oldvars:
# this copies the value from the old file if the key exists in there
value = oldvars[key]
else:
# ask the user for a new value
value = raw_input("Enter the value ")
# update the value in newvars
newvars[key] = value
ruamel.yaml.round_trip_dump(newvars, sys.stdout)
给你:
Enter the value abcd
# Enter the release and build you wish to deploy
release: '4.0'
build: '4_0_178'
ems_release: '4.0'
ems_build: '4_0_999'
build_type: test_build
# The name prefix is synonymous with the stack (i.e. pdx02-cloud-prod)
name_prefix: syd01-devops-deepali
# The deployment type is one of: [ test | trial | dev | prod ]
deployment_type: trial
# deployment_url is typically the same value as deployment type unless it is a premium deployment.
# In that case deployment_type is set to prod and deployment_url is either dev, trial or test
deployment_url: '{{ deployment_type }}'
some_new_var: abcd
请注意: