简体   繁体   English

如何使用 ruamel.yaml 将内容添加到 YAML 文件?

[英]How to add contents to YAML file using ruamel.yaml?

Here is the input.yml file这是 input.yml 文件

PRODUCT_HOME: /app
config: 
  active-profiles: mysql,oauth2
  driverClassName: com.mysql.cj.jdbc.Driver
  datasourceurl: jdbc:h2:file:./data
  datasourceuser: sa
server:
  error:
    path: /error
  whitelabel:
    enabled: false
  port: 8080

Output file should look like: Output 文件应如下所示:

PRODUCT_HOME: /app
config: 
  active-profiles: mysql,oauth2
  driverClassName: com.mysql.cj.jdbc.Driver
  datasourceurl: jdbc:h2:file:./data
  datasourceuser: sa
server:
   error:
      path: /error
   whitelabel:
      enabled: false
   port: 8080
   servlet:
     session:
       cookie:
         secure: true

How can I achieve this using Python (ruamel.yaml) package?如何使用 Python (ruamel.yaml) package 实现此目的?

You should create the correct data structure for the data you want to add and then add that datastructure to the mapping that is the value of server您应该为要添加的数据创建正确的数据结构,然后将该数据结构添加到作为server值的映射

import sys
import ruamel.yaml

from pathlib import Path

in_file = Path('input.yaml')
    
nd = dict(servlet=dict(session=dict(cookie=dict(secure=True))))

yaml = ruamel.yaml.YAML()
data = yaml.load(in_file)
data['server'].update(nd)

# print(data)
yaml.dump(data, sys.stdout)

which gives:这使:

PRODUCT_HOME: /app
config:
  active-profiles: mysql,oauth2
  driverClassName: com.mysql.cj.jdbc.Driver
  datasourceurl: jdbc:h2:file:./data
  datasourceuser: sa
server:
  error:
    path: /error
  whitelabel:
    enabled: false
  port: 8080
  servlet:
    session:
      cookie:
        secure: true

Alternatively, if you have the data to add as YAML input you can load that and then update:或者,如果您有要添加为 YAML 输入的数据,您可以加载它然后更新:

yaml_str = """
servlet:
  session:
    cookie:
      secure: true
"""

yaml = ruamel.yaml.YAML()
nd = yaml.load(yaml_str)
data = yaml.load(in_file)
data['server'].update(nd)

# print(data)
yaml.dump(data, sys.stdout)

which also gives:这也给出了:

PRODUCT_HOME: /app
config:
  active-profiles: mysql,oauth2
  driverClassName: com.mysql.cj.jdbc.Driver
  datasourceurl: jdbc:h2:file:./data
  datasourceuser: sa
server:
  error:
    path: /error
  whitelabel:
    enabled: false
  port: 8080
  servlet:
    session:
      cookie:
        secure: true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用ruamel.yaml自动添加参考? - How to automatically add a reference with ruamel.yaml? 如何使用ruamel.yaml添加节点 - How to add a node with ruamel.yaml 使用 Pyyaml 或 Ruamel.yaml 在 Python 中编辑 .yaml 文件 - Editing a .yaml file in Python using Pyyaml or Ruamel.yaml 如何使用 ruamel.yaml 在 YAML 文件中保留或添加值周围的引号而不剥离注释作为副作用? - How can I preserve or add quotes around values in a YAML file without stripping comments as a side effect using ruamel.yaml? 如何使用ruamel.yaml注释掉YAML部分? - How to comment out a YAML section using ruamel.yaml? 如何使用ruamel.yaml在Python中向YAML插入注释行? - How to insert a comment line to YAML in Python using ruamel.yaml? 如何使用 ruamel.yaml 包编辑 yaml 文件,而不更改文件的缩进? - How do I edit a yaml file using ruamel.yaml package, without changing the indentation of the file? 如何使用ruamel.yaml而不是pyyaml通过python编辑YAML文件? - How do I edit a YAML file through python using ruamel.yaml and not pyyaml? 如何在Python中使用ruamel.yaml从YAML文件中获取注释? - How can I get comments from a YAML file using ruamel.yaml in Python? 如何读取 YAML 文件中的组件以便我可以使用 ruamel.yaml 编辑它的键值? - How to read a component in YAML file so that I can edit it's key value using ruamel.yaml?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM