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

parse and update YAML file at particular position in python

我的 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
        GITHUB_PRIVATE_KEY: |
          -----BEGIN RSA PRIVATE KEY-----
          M8rG0u9pa6rdALV1DzV2jM5EwFg8w8KkBz+iXBZty4oKYYGirJ26h/XtJmJUCAwE
          HEn0KXVI/8myJLcBIRE7QCOKJI76SzBtkNShZimN3nGfoecoVR5zlJdHDNbJ7LsL
          M8rG0u9pa6rdALV1DzV2jM5EwFg8w8KkBz+iXBZty4oKYYGirJ26h/XtJmJUCAwE
          HEn0KXVI/8myJLcBIRE7QCOKJI76SzBtkNShZimN3nGfoecoVR5zlJdHDNbJ7LsL
          M8rG0u9pa6rdALV1DzV2jM5EwFg8w8KkBz+iXBZty4oKYYGirJ26h/XtJmJUCAwE
          HEn0KXVI/8myJLcBIRE7QCOKJI76SzBtkNShZimN3nGfoecoVR5zlJdHDNbJ7LsL
          M8rG0u9pa6rdALV1DzV2jM5EwFg8w8KkBz+iXBZty4oKYYGirJ26h/XtJmJUCAwE
          HEn0KXVI/8myJLcBIRE7QCOKJI76SzBtkNShZimN3nGfoecoVR5zlJdHDNbJ7LsL
          M8rG0u9pa6rdALV1DzV2jM5EwFg8w8KkBz+iXBZty4oKYYGirJ26h/XtJmJUCAwE
          HEn0KXVI/8myJLcBIRE7QCOKJI76SzBtkNShZimN3nGfoecoVR5zlJdHDNbJ7LsL
          M8rG0u9pa6rdALV1DzV2jM5EwFg8w8KkBz+iXBZty4oKYYGirJ26h/XtJmJUCAwE
          HEn0KXVI/8myJLcBIRE7QCOKJI76SzBtkNShZimN3nGfoecoVR5zlJdHDNbJ7LsL
          M8rG0u9pa6rdALV1DzV2jM5EwFg8w8KkBz+iXBZty4oKYYGirJ26h/XtJmJUCAwE
          HEn0KXVI/8myJLcBIRE7QCOKJI76SzBtkNShZimN3nGfoecoVR5zlJdHDNbJ7LsL
          M8rG0u9pa6rdALV1DzV2jM5EwFg8w8KkBz+iXBZty4oKYYGirJ26h/XtJmJUCAwE
          HEn0KXVI/8myJLcBIRE7QCOKJI76SzBtkNShZimN3nGfoecoVR5zlJdHDNbJ7LsL
          M8rG0u9pa6rdALV1DzV2jM5EwFg8w8KkBz+iXBZty4oKYYGirJ26h/XtJmJUCAwE
          HEn0KXVI/8myJLcBIRE7QCOKJI76SzBtkNShZimN3nGfoecoVR5zlJdHDNbJ7LsL
          M8rG0u9pa6rdALV1DzV2jM5EwFg8w8KkBz+iXBZty4oKYYGirJ26h/XtJmJUCAwE
          HEn0KXVI/8myJLcBIRE7QCOKJI76SzBtkNShZimN3nGfoecoVR5zlJdHDNbJ7LsL
          M8rG0u9pa6rdALV1DzV2jM5EwFg8w8KkBz+iXBZty4oKYYGirJ26h/XtJmJUCAwE
          HEn0KXVI/8myJLcBIRE7QCOKJI76SzBtkNShZimN3nGfoecoVR5zlJdHDNbJ7LsL
          M8rG0u9pa6rdALV1DzV2jM5EwFg8w8KkBz+iXBZty4oKYYGirJ26h/XtJmJUCAwE
          HEn0KXVI/8myJLcBIRE7QCOKJI76SzBtkNShZimN3nGfoecoVR5zlJdHDNbJ7LsL
          M8rG0u9pa6rdALV1DzV2jM5EwFg8w8KkBz+iXBZty4oKYYGirJ26h/XtJmJUCAwE
          HEn0KXVI/8myJLcBIRE7QCOKJI76SzBtkNShZimN3nGfoecoVR5zlJdHDNbJ7LsL
          -----END RSA PRIVATE KEY-----
      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 条件结束。有人可以在这里帮助我..! 我通过添加 GITHUB_PRIVATE_KEY 更新了 yaml 文件; @RoadRunner 的方法无法解析此 GITHUB_PRIVATE_KEY。在生成的最终 YAML 中,它尝试包含单引号并且可以在 GITHUB_PRIVATE_KEY

中看到新行

您可以使用 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_rec(iterable, search_key, data):
    if isinstance(iterable, list):
        for item in iterable:
            if isinstance(item, (list, dict)):
                insert_data_rec(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_rec(v, search_key, data)

然后调用这个函数修改data,输出新的内容safe_dump():

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

print(yaml.safe_dump(data))

输出如下data.