解析并更新python中特定位置的YAML文件

问题描述 投票:0回答:1

我的Yaml文件如下所示:

jobs:
- build_logs_to_retain: 1000
  name: demo-comp2
  plan:
  - get: landscape
    passed:
    - demo-comp1
    trigger: true
  - attempts: 3
    config:
      container_limits: {}
      image_resource:
        source:
          password: ""
          repository: ubuntu
          username: ""
        type: docker-image
      inputs:
      - name: landscape
      params:
        INFLUXDB_DATABASE: newdatabase
        INFLUXDB_PASSWORD: 123456789abcdefgh
        INFLUXDB_URL: influxdb.com
        INFLUXDB_USER: newuser
      platform: linux
      run:
        path: my_path/my_path/script.sh
    task: task1
    timeout: 120m
  - attempts: 3
    config:
      container_limits: {}
      image_resource:
        source:
          password: ""
          repository: ubuntu
          username: ""
        type: docker-image
      inputs:
      - name: landscape
      params:
        INFLUXDB_DATABASE: newdatabase
        INFLUXDB_PASSWORD: 123456789abcdefgh
        INFLUXDB_URL: influxdb.com
        INFLUXDB_USER: newuser
      platform: linux
      run:
        path: my_path/my_path/script.sh
    task: test-demo-comp2
    timeout: 20m
  serial: true
- build_logs_to_retain: 1000
  name: demo-comp1
  plan:
  - get: landscape
    trigger: true
  - attempts: 3
    config:
      container_limits: {}
      image_resource:
        source:
          password: ""
          repository: ubuntu
          username: ""
        type: docker-image
      inputs:
      - name: landscape
      params:
        INFLUXDB_DATABASE: newdatabase
        INFLUXDB_PASSWORD: 123456789abcdefgh
        INFLUXDB_URL: influxdb.com
        INFLUXDB_USER: newuser
      platform: linux
      run:
        path: my_path/my_path/script.sh
    task: deploy-demo-comp1
    timeout: 120m
  - attempts: 3
    config:
      container_limits: {}
      image_resource:
        source:
          password: ""
          repository: ubuntu
          username: ""
        type: docker-image
      inputs:
      - name: landscape
      params:
        INFLUXDB_DATABASE: newdatabase
        INFLUXDB_PASSWORD: 123456789abcdefgh
        INFLUXDB_URL: influxdb.com
        INFLUXDB_USER: newuser
      platform: linux
      run:
        path: my_path/my_path/script.sh
    task: test-demo-comp1
    timeout: 20m
  serial: true
resources:
- check_every: 5m
  name: landscape
  source:
    branch: master
    password: newpassword
    paths:
    - config/*
    - config/**/*
    uri: my_repo.git
    username: myuser
  type: git

我正在尝试解析此YAML并在params部分下添加新的key:values。当前params具有以下内容:

INFLUXDB_DATABASE: newdatabase
INFLUXDB_PASSWORD: 123456789abcdefgh
INFLUXDB_URL: influxdb.com
INFLUXDB_USER: newuser

并且我正在尝试添加新的kay:value对MY_NAME:Rohith;这样,params下的内容如下所示:

INFLUXDB_DATABASE: newdatabase
INFLUXDB_PASSWORD: 123456789abcdefgh
INFLUXDB_URL: influxdb.com
INFLUXDB_USER: newuser
MY_NAME: Rohith

我正在python3中尝试这个;自我解析,我结束了太多的for循环和if条件。有人可以在这里帮助我..!

python-3.x yaml pyyaml
1个回答
0
投票

您可以使用PyYAML使用safe_load()将YAML文件读入python对象:

import yaml

with open("data.yml") as yml_file:
    data = yaml.safe_load(yml_file)

您可以使用pip install PyYAML安装此库。

然后,您可以创建一个递归函数以遍历python对象,并为update一个字典,其中包含您传递给它的数据,并提供搜索键:

def insert_data(iterable, search_key, data):
    if isinstance(iterable, list):
        for item in iterable:
            insert_data(item, search_key, data)

    elif isinstance(iterable, dict):
        for k, v in iterable.items():
            if k == search_key:
                iterable[k].update(data)
            if isinstance(v, (list, dict)):
                insert_data(v, search_key, data)

然后您可以像这样调用它并修改data并使用safe_dump()输出新内容:

insert_data(data, search_key='params', data={'MY_NAME': 'Rohith'})

print(yaml.safe_dump(data))

输出以下data

© www.soinside.com 2019 - 2024. All rights reserved.