比较密钥后,将内容从一个 YAML 复制到另一个 YAML [英] Copy content from one YAML to another YAML after comparison of keys

查看:22
本文介绍了比较密钥后,将内容从一个 YAML 复制到另一个 YAML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用例,我需要从新的 YAML 文件中选择键:值对.检查该键是否存在于旧的 YAML 文件中,是否确实复制了该值并将其设置在新的 YAML 文件中.
此外,如果旧文件中不存在该密钥,则询问用户.

I have a use case where I need to pick key:value pairs from a new YAML file. Check whether that key exists in old YAML file and if it does copy the value and set it in new YAML file.
Also if that key does not exist in old file then ask the user.

代码:

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 ")

它不起作用.无法理解如何检查旧文件中的密钥并将该密钥的值写入新文件中.

It's not working. Not able to understand how to check the key in old file and write value for that key in new file.

新文件:

# 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 }}"

预期:使用两个文件(旧文件和新文件)生成的文件

Expected: File generated using two files(old and new)

# 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

推荐答案

这是无效的 Python,您的 try 没有匹配的 except.除此之外,没有必要在用于读取旧"文件的 with 语句的上下文中打开第二个文件.因此开始:

That is invalid Python, your try doesn't have a matching except. Apart from that it is not necessary to open the second file in the context of the with statement that you use for reading in the "old" file. Therefore start 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' 完成的):

You cannot open a YAML file for reading and writing, therefore just read it (and opening for reading and writing of a file is done with 'r+' not with 'rw'):

with open("/home/ubuntu/tmp/deploy/all", 'r') as f2:
    newvars = ruamel.yaml.round_trip_load(f2)

之后继续不缩进并在适当的情况下从旧变量更新新变量:

After that continue unindented and update newvars from oldvars if appropriate:

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.yamlnew.yaml 并用abcd"回答提示:

Combining that and naming your files old.yaml and new.yaml and answering the prompt with '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

请注意:

  • 这仅在 Python 2.7 上运行
  • 对于不需要它们的标量,引号被删除
  • 我没有尝试将输出的四个空格缩进包含在第一行之后.

这篇关于比较密钥后,将内容从一个 YAML 复制到另一个 YAML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆