如何使用PYYAML更新yaml文件?

发布于 2025-01-23 03:37:52 字数 447 浏览 2 评论 0原文

我如何

使用Python(PyyAml软件包)评论“ driverClassName ”行和更新 org.quartz.threadpool.threadcount 200至200至200?

PRODUCT_HOME: /app
config: 
  active-profiles: mysql,oauth2
  driverClassName: com.mysql.cj.jdbc.Driver
  datasourceurl: jdbc:h2:file:./data
  datasourceuser: sa
spring:
  quartz:
  job-store-type: jdbc
  enabled: true
  properties: 
     org.quartz.threadPool.threadCount: 50

How can I comment the 'driverClassName' line and update org.quartz.threadPool.threadCount to 200

In the YAML snippet below, using python (PyYAML package)?

PRODUCT_HOME: /app
config: 
  active-profiles: mysql,oauth2
  driverClassName: com.mysql.cj.jdbc.Driver
  datasourceurl: jdbc:h2:file:./data
  datasourceuser: sa
spring:
  quartz:
  job-store-type: jdbc
  enabled: true
  properties: 
     org.quartz.threadPool.threadCount: 50

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

调妓 2025-01-30 03:37:52

Pyyaml无法添加评论。它实际上删除了有关加载的所有评论,因此一般而言Pyyaml
不是需要进一步人类互动的YAML文档的合适工具(这
我认为是添加评论的原因)。

您应该使用ruamel.yaml(免责声明:我是该软件包的作者)

import sys
import ruamel.yaml

yaml_str = """\
PRODUCT_HOME: /app
config: 
  active-profiles: mysql,oauth2
  driverClassName: com.mysql.cj.jdbc.Driver
  datasourceurl: jdbc:h2:file:./data
  datasourceuser: sa
spring:
  quartz:
  job-store-type: jdbc
  enabled: true
  properties: 
     org.quartz.threadPool.threadCount: 50
"""
    
yaml = ruamel.yaml.YAML()
data = yaml.load(yaml_str)
data['config'].yaml_add_eol_comment('this is the SQL driver', 'driverClassName', column=50)
data['spring']['properties']['org.quartz.threadPool.threadCount'] = 200
yaml.dump(data, sys.stdout)

PRODUCT_HOME: /app
config:
  active-profiles: mysql,oauth2
  driverClassName: com.mysql.cj.jdbc.Driver       # this is the SQL driver
  datasourceurl: jdbc:h2:file:./data
  datasourceuser: sa
spring:
  quartz:
  job-store-type: jdbc
  enabled: true
  properties:
    org.quartz.threadPool.threadCount: 200

PyYAML cannot add comments. It actually removes all comments on loading, so in general PyYAML
is not the appropriate tool for YAML documents that need further human interaction (which
I assume is the reason for adding a comment).

You should do this with ruamel.yaml (disclaimer: I am the author of that package):

import sys
import ruamel.yaml

yaml_str = """\
PRODUCT_HOME: /app
config: 
  active-profiles: mysql,oauth2
  driverClassName: com.mysql.cj.jdbc.Driver
  datasourceurl: jdbc:h2:file:./data
  datasourceuser: sa
spring:
  quartz:
  job-store-type: jdbc
  enabled: true
  properties: 
     org.quartz.threadPool.threadCount: 50
"""
    
yaml = ruamel.yaml.YAML()
data = yaml.load(yaml_str)
data['config'].yaml_add_eol_comment('this is the SQL driver', 'driverClassName', column=50)
data['spring']['properties']['org.quartz.threadPool.threadCount'] = 200
yaml.dump(data, sys.stdout)

which gives:

PRODUCT_HOME: /app
config:
  active-profiles: mysql,oauth2
  driverClassName: com.mysql.cj.jdbc.Driver       # this is the SQL driver
  datasourceurl: jdbc:h2:file:./data
  datasourceuser: sa
spring:
  quartz:
  job-store-type: jdbc
  enabled: true
  properties:
    org.quartz.threadPool.threadCount: 200
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文