我正在尝试创建 yaml
import sys
from ruamel.yaml import YAML
class Entry:
yaml_tag = '!<!entry>'
def __init__(self, value, style=None):
self.value = value
self.style = style
@classmethod
def to_yaml(cls, representer, node):
return representer.represent_mapping(cls.yaml_tag, node.value, node.style)
@classmethod
def from_yaml(cls, constructor, node):
return cls(node.value, node.style)
data = {
'steps': [
Entry({
'id': 'Entry-1',
'actions': [],
})
],
}
yaml = YAML(typ='rt')
yaml.register_class(Entry)
yaml.dump(data, sys.stdout)
但在输出中,!<!entry>我得到了!%3C%21entry%3E.
steps:
- !%3C%21entry%3E
id: Entry-1
actions: []
此处建议的解决方案:https
://stackoverflow.com/a/72256797/8058496 目前无法使用绝对标签。ruamel.yaml 可以读取它们但不能渲染它们。
他们建议对输出进行后处理以替换标签的开头和结尾:
结果我们得到: