问题描述
我正在使用Python在YAML文件的列表中动态添加元素,我想在要添加的每个元素旁边添加一个注释.以下是所有所需的格式:
I am dynamically adding elements in a list in a YAML file using Python and I would like to add a comment alongside each of the elements I am adding. The following are all desired formats:
flow_style_example:
- [a, b, c] # first list
- [d, e] # second list
block_style_example:
- - a # first list side comment
- b
- c
# second list top comment
- - d
- e
list_of_elements_side_comment:
- a # foo
- b # bar
list_of_elements_top_comment:
# comment 1
- a
# comment 2
- b
对于上述任何一项,我还没有弄清楚如何正确创建相应的CommentToken
条目,尤其是在标记方面(如何确定刚刚添加的内容的行和列?)
For any of the above I have yet to figure out how to properly create the respective CommentToken
entries, especially when it comes to marks (how to determine the line and col of what was just added?)
如何实现上述功能的 任何 ?
How can I achieve any of the above functionalities?
推荐答案
与其在您的问题中写下您将要欣赏的内容,不如说是有用的查看程序,确定做错了什么.
Instead of writing in your question what you would appreciate, it would have been more usefulto see your program, to determine what you were doing wrong.
由于混合使用缩进样式,因此无法获得准确的缩进想要一次转储.
Because you mix and match indentation styles, you cannot get the exact indentation youwant in one dump.
import sys
import ruamel.yaml
CS = ruamel.yaml.comments.CommentedSeq # defaults to block style
CM = ruamel.yaml.comments.CommentedMap # defaults to block style
def FS(x): # flow style list
res = CS(x)
res.fa.set_flow_style()
return res
yaml = ruamel.yaml.YAML()
yaml.indent(sequence=4, offset=2)
lst = CS()
lst.append(FS(['a', 'b', 'c']))
lst.append(FS(['d', 'e']))
lst.yaml_add_eol_comment("first list", 0, 0)
lst.yaml_add_eol_comment("second list\n\n", 1)
data = CM(flow_style_example=lst)
lst = CS()
data['block_style_example'] = lst
lst.append(CS(['a', 'b', 'c']))
lst[0].yaml_add_eol_comment("first list side comment", 0, 0)
lst.append(CS(['d', 'e']))
lst.yaml_set_comment_before_after_key(1, "second list top comment", 2)
lst = CS(['a', 'b'])
lst.yaml_add_eol_comment("foo", 0, 0)
lst.yaml_add_eol_comment("bar\n\n", 1)
data["list_of_elements_side_comment"] = lst
data.yaml_set_comment_before_after_key("list_of_elements_side_comment", "\n")
lst = CS(['a', 'b'])
lst.yaml_set_comment_before_after_key(0, "comment 1", 2)
lst.yaml_set_comment_before_after_key(1, "comment 2", 2)
data["list_of_elements_top_comment"] = lst
yaml.dump(data, sys.stdout)
给出:
flow_style_example:
- [a, b, c] # first list
- [d, e] # second list
block_style_example:
- - a # first list side comment
- b
- c
# second list top comment
- - d
- e
list_of_elements_side_comment:
- a # foo
- b # bar
list_of_elements_top_comment:
# comment 1
- a
# comment 2
- b
CommentedSeq
的注释处理与CommentedMap
:注释当前存储为字典,其中序列索引执行与映射键相同的功能,因此在序列/列表上使用yaml_set_comment_before_after_key
.
The comment handling for CommentedSeq
is very similar to that ofCommentedMap
: comments are currently stored as a dict where thesequence index fulfills the same function as the mapping key, hencethe use of yaml_set_comment_before_after_key
on a sequence/list.
上面使用的是ruamel.yaml的内部结构,如果没有通知CQ.有通知,但没有引起您的注意.因此(准备来修复您安装的ruamel.yaml的版本号.
The above uses internals of ruamel.yaml, which might change withoutnotice cq. with notice, but without you noticing. Therefore (be preparedto) fix the version number of ruamel.yaml you install.
这篇关于在ruamel.yaml中的list元素中添加评论的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!