- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我想从使用 ruamel.yaml
加载的 YAML 文件中获取评论字符串。 project documentation缺少 API 引用,我找不到相关示例。访问评论的正确方法是什么?
import ruamel.yaml
yaml = """\
%YAML 1.2
---
# C1
a: # C2
# C3
# C4
b: 1 # C5
c: # A comment here will not be parsed properly by ruamel.yaml v0.11.14
- abc # C6
- xyz # C7
# C8
# C9
"""
loaded = ruamel.yaml.round_trip_load(yaml)
# Now what?
最佳答案
库作者在 an issue on BitBucket 中对此发表评论(2016 年 5 月 9 日):
The comment preservation has not stabilized, e.g. I need to do something if the key and value of a mapping are not on the same line and the key (or both key and value) have a comment. And my initial target was preservation of existing comments, not so much manipulation.
通过一些实验,我确定了上述问题中提供的示例代码的以下工作:
print('Comment 1: ' + loaded.ca.comment[1][0].value)
print('Comment 2: ' + loaded.ca.items['a'][2].value)
print('Comment 3: ' + loaded.ca.items['a'][3][0].value)
print('Comment 4: ' + loaded.ca.items['a'][3][1].value)
print('Comment 5: ' + loaded['a'].ca.items['b'][2].value)
print('Comment 6: ' + loaded['a']['c'].ca.items[0][0].value)
print('Comment 7: ' + loaded['a']['c'].ca.items[1][0].value)
print('Comment 8: ' + loaded['a']['c'].ca.end[0].value)
print('Comment 9: ' + loaded['a']['c'].ca.end[1].value)
关于python - 如何在 Python 中使用 ruamel.yaml 从 YAML 文件中获取注释?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38252507/
我似乎无法弄清楚如何使用 Ruamel.yaml 在数据之间添加一个空行。 假设我有数据: --- a: 1 b: 2 我需要添加到这一点,以便我将有: --- a: 1 b: 2 c: 3 我知道空
我正在使用 Ruamel Python 库以编程方式编辑人工编辑的 YAML 文件。 我很难理解如何将评论插入结构化数据。 我有一些数据: a: b: banana c: apple d:
我正在使用 Ruamel Python 库以编程方式编辑人工编辑的 YAML 文件。源文件具有按字母顺序排序的键。 我不确定这是一个基本的 Python 问题还是一个 Ruamel 问题,但是我尝试对
如果我有多个引用,当我使用 Python 中的 ruaml.yaml 将它们写入 YAML 文件时,我得到: helptexts: - &help-one help_text: |
我有一个使用 ruamel.yaml 和 ruamel.yaml.jinja2 一段时间的 docker 容器。最近它开始失败了。以下 dockerfile 重现了该问题: FROM ubuntu R
这里是python版本: Python 3.6.5 我想安装 ruamel: pip3 install ruamel 但我无法安装它: (venvpython3) d3alg@ubuntu-59:/$
我跑了一个 pip install ruamel.yaml 当我尝试做的时候 import ruamel.yaml 我得到一个错误: ImportError: No module named ruam
ruamel.yaml 文档很少,我在这里没有找到我需要的响应。 my_file.yaml 的内容: desc: "blahblahblah" 问题1:如何(以编程方式)将“size”添加为与“des
我需要对带有注释的 YAML 文件进行排序。我正在使用 ruamel.yaml 库来保留 YAML 中的注释,但是当我对项目进行排序时,注释被放置在错误的位置。 people = """\ # man
我希望使用 ruamel.yaml 对大型人工编辑的 YAML 文件执行一些自动编辑。 输入文件包含合并键,如下所示: foo: &foo color: red bar: name: qux
我想在我的项目中使用 Ruamel YAML,我使用代理,所以我下载了 来自https://pypi.python.org/pypi/ruamel.yaml的gzip包并安装它 pip install
我正在尝试使用 ruamel.yaml 使用 python 动态修改 AWS CloudFormation 模板。我添加了以下代码以使 safe_load 与 CloudFormation 函数(例如
在哪里可以找到 Python 包的完整文档 ruamel.yaml哪个加载和转储 YAML 文件? Python Package Index 仅给出了如何使用注释的示例。我正在尝试了解如何整体使用该包
我有两个问题! 有没有办法防止 round_trip_dump 甚至只是常规转储在句子中间断行?每当我的 YAML 文件中有一个长句子(即描述),并且我使用脚本来更改一些内容时,它就会断行并破坏我的文
我想转储一个包含嵌套列表组件的字典,其中列表的每一行都在自己的行上。我还想保持字典顺序。 这是一个 MWE: import sys from ruamel import yaml from ruame
MWE: import sys from ruamel.yaml import YAML yaml = YAML(typ='safe') yaml.default_flow_style = False
使用 Python 3.4.7,ruamel.yaml 版本 0.15.35,以下内容对我不起作用: import sys import enum import ruamel.yaml from ru
我正在使用 Ruamel 来保留人工编辑的 YAML 文件中的引用样式。 我有示例输入数据: --- a: '1' b: "2" c: 3 我使用以下方式读入数据: def read_file(f):
我正在使用 ruamel.yaml 来解析复杂的 YAML 文档,其中某些标记的节点需要特殊处理。按照已发布示例的建议,我使用 add_multi_constructor 注入(inject)自定义解
给出以下代码: from ruamel.yaml import YAML yaml = YAML() with open(filename) as f: z = yaml.load(f) ya
我是一名优秀的程序员,十分优秀!