如何解决在 YAML 中显示逐字标记的问题?
How to fix problems with displaying verbatim tag in YAML?
我在转储带有逐字标记的 YAML 文件时遇到问题。我该如何解决编码问题?
我尝试通过这种方式转储 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: []
目前无法使用绝对标签。 ruamel.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': [],
})
],
}
def post(s):
PAT = '!%3C%21'
res = []
for line in s.splitlines(True):
while PAT in line:
start, rest = line.split(PAT, 1)
line = start + '!<!' + (rest.replace('%3E', '>'))
res.append(line)
return ''.join(res)
yaml2 = YAML(typ='rt')
yaml2.register_class(Entry)
yaml2.dump(data, sys.stdout, transform=post)
给出:
steps:
- !<!entry>
id: Entry-1
actions: []
它应该能够处理同一输出行上的多个标签。
虽然有点丑,但至少看起来是有效的。
我在转储带有逐字标记的 YAML 文件时遇到问题。我该如何解决编码问题?
我尝试通过这种方式转储 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: []
目前无法使用绝对标签。 ruamel.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': [],
})
],
}
def post(s):
PAT = '!%3C%21'
res = []
for line in s.splitlines(True):
while PAT in line:
start, rest = line.split(PAT, 1)
line = start + '!<!' + (rest.replace('%3E', '>'))
res.append(line)
return ''.join(res)
yaml2 = YAML(typ='rt')
yaml2.register_class(Entry)
yaml2.dump(data, sys.stdout, transform=post)
给出:
steps:
- !<!entry>
id: Entry-1
actions: []
它应该能够处理同一输出行上的多个标签。 虽然有点丑,但至少看起来是有效的。