如何使用ruamel.yaml添加评论 [英] How can I add a comment with ruamel.yaml

查看:51
本文介绍了如何使用ruamel.yaml添加评论的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用ruamel.yaml创建数据结构,并想在转储和/或再次加载之前添加注释.不幸的是,所有示例都首先使用往返转储程序加载一些字符串,或者不再使用不再存在的API.

这就是我要转储的内容:

  test:asdf#测试注释! 

我尝试了以下操作:

  insert = ruamel.yaml.comments.CommentedMap()start_mark = ruamel.yaml.error.CommentMark(0)insert ['test'] ='asdf'insert.ca.items ['test'] = [无,[ruamel.yaml.CommentToken(值='#测试注释!',开始标记=开始标记,结束标记=无)],没有任何,没有任何]ruamel.yaml.round_trip_dump(插入,sys.stdout) 

打印.

 #测试注释!test:asdf 

以某种方式,注释位于值的前面而不是后面.我在做什么错了?

解决方案

您可以尝试通过添加 CommentTokens 来做到这一点,但是start_mark类型必须来自 ruamel.yaml.error .

插入设置为 CommentedMap (这很容易)映射将在进行正常的往返加载时加载),然后使用其 yaml_add_eol_comment 方法:

  import sys进口ruamel.yaml插入= ruamel.yaml.comments.CommentedMap()insert ['test'] ='asdf'insert.yaml_add_eol_comment('测试注释!','测试',列= 0)yaml = ruamel.yaml.YAML()#yaml.indent(映射= 4,序列= 4,偏移= 2)yaml.dump(插入,sys.stdout) 

给出:

  test:asdf#测试注释! 

column = 0 是可选的.如果您未指定开始列,您会在#之前获得两个空格,0会尝试将其一直推到最前面,但是键-值对就在前面.

您可以在您为 yaml_add_eol_comment 方法指定的EOL注释,但如果它不在那里,会被添加.

如果您在多个按键之后有多个评论,请点击行,并希望您的注释对齐,您只需指定添加的第一个列.

I'm trying to create a data structure with ruamel.yaml and want to add comments before dumping and/or loading it again. Unfortunately all examples load some string with the round trip dumper first or use no longer existing APIs.

This is what I am trying to dump:

test: asdf # Test Comment!

I tried the following:

insert = ruamel.yaml.comments.CommentedMap()
start_mark = ruamel.yaml.error.CommentMark(0)
insert['test'] = 'asdf'
insert.ca.items['test'] = [ None,
    [ruamel.yaml.CommentToken(value='# Test Comment!', start_mark=start_mark, end_mark=None)],
    None,
    None
]
ruamel.yaml.round_trip_dump(insert, sys.stdout)

which prints.

# Test Comment!test: asdf

Somehow the comment is in front instead of behind the values. What am I doing wrong?

解决方案

You can try to do this by adding the CommentTokens, but the start_mark type would have to be comming from ruamel.yaml.error.

It is much more easy to make insert a CommentedMap (which is what the mapping would get loaded as, when doing a normal, round-trip, load), and then use its yaml_add_eol_comment method:

import sys
import ruamel.yaml


insert = ruamel.yaml.comments.CommentedMap()
insert['test'] = 'asdf'
insert.yaml_add_eol_comment('Test Comment!', 'test', column=0)

yaml = ruamel.yaml.YAML()
# yaml.indent(mapping=4, sequence=4, offset=2)
yaml.dump(insert, sys.stdout)

which gives:

test: asdf # Test Comment!

The column=0 is optional. If you don't specify the start column, you'll get two spaces before the #, 0 tries to push it all the way to the front, but of course the key-value pair is in the way.

You can specify the # in the EOL comment you specify to the yaml_add_eol_comment method, but if it is not there, it will be prepended.

If you have multiple comments after multiple keys on consecurive lines, and want your comments to align, you only have to specify the column of the first one added.

这篇关于如何使用ruamel.yaml添加评论的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆