如果我使用感叹号,为什么我在 yaml 文件的输出中得到引号?
Why I get quotes in the output of yaml file if I use exclamation mark?
我创建了一个名为 main.yaml 的 yaml 文件,但我希望输出如下:
# example
Name:
Job: abcd
Addresss: xyz
client_id: 641
test: !include test.yaml # new key
我不想在输出中使用引号。那么如何防止引号出现在输出中呢?
----------------------------代码----------
import sys
from pathlib import Path
from ruamel.yaml import YAML
from ruamel.yaml.scalarstring import SingleQuotedScalarString, DoubleQuotedScalarString
inp = """\
# example
Name:
Job: abcd
Addresss: xyz
client_id: 641
"""
opath2= Path('main.yaml')
with YAML(output=opath2) as yaml:
yaml.indent(sequence=4, offset=2)
code = yaml.load(inp)
code.insert(1, 'test', '!include test.yaml', comment="new key")
yaml.dump(code, sys.stdout)
----------------------------输出------------ --------------
# example
Name:
Job: abcd
Addresss: xyz
client_id: 641
test: '!include test.yaml' # new key
I don’t want quotes in the output. So how to prevent the quotes from appearing in the output?
在 YAML 中,字符串只有在没有其他含义时才可以不加引号。 124
是数字,不是字符串。 true
是布尔值,不是字符串。 !foo
表示 tag,不是字符串。因此,如果你想要一个以感叹号开头的不带引号的字符串,那你就不走运了,在 YAML 中做不到。
如果你真的想插入一个标签,就像deceze认为的那样,你需要做一些工作。最简单的方法是实际实现一个 class 来表示。
import ruamel
class Include(ruamel.yaml.YAMLObject):
yaml_constructor = ruamel.yaml.RoundTripConstructor
yaml_representer = ruamel.yaml.RoundTripRepresenter
yaml_tag = '!include'
def __init__(self, file):
self.file = file
@classmethod
def from_yaml(cls, loader, node):
return cls(loader.construct_scalar(node))
@classmethod
def to_yaml(cls, dumper, data):
if isinstance(data.file, ruamel.yaml.scalarstring.ScalarString):
style = data.file.style # ruamel.yaml>0.15.8
else:
style = None
return dumper.represent_scalar(cls.yaml_tag, data.file, style=style)
(无耻地抄袭自ruamel/_test/test_add_xxx.py
。)然后你就可以
code.insert(1, 'test', Include('test.yaml'), comment="new key")
我创建了一个名为 main.yaml 的 yaml 文件,但我希望输出如下:
# example
Name:
Job: abcd
Addresss: xyz
client_id: 641
test: !include test.yaml # new key
我不想在输出中使用引号。那么如何防止引号出现在输出中呢?
----------------------------代码----------
import sys
from pathlib import Path
from ruamel.yaml import YAML
from ruamel.yaml.scalarstring import SingleQuotedScalarString, DoubleQuotedScalarString
inp = """\
# example
Name:
Job: abcd
Addresss: xyz
client_id: 641
"""
opath2= Path('main.yaml')
with YAML(output=opath2) as yaml:
yaml.indent(sequence=4, offset=2)
code = yaml.load(inp)
code.insert(1, 'test', '!include test.yaml', comment="new key")
yaml.dump(code, sys.stdout)
----------------------------输出------------ --------------
# example
Name:
Job: abcd
Addresss: xyz
client_id: 641
test: '!include test.yaml' # new key
I don’t want quotes in the output. So how to prevent the quotes from appearing in the output?
在 YAML 中,字符串只有在没有其他含义时才可以不加引号。 124
是数字,不是字符串。 true
是布尔值,不是字符串。 !foo
表示 tag,不是字符串。因此,如果你想要一个以感叹号开头的不带引号的字符串,那你就不走运了,在 YAML 中做不到。
如果你真的想插入一个标签,就像deceze认为的那样,你需要做一些工作。最简单的方法是实际实现一个 class 来表示。
import ruamel
class Include(ruamel.yaml.YAMLObject):
yaml_constructor = ruamel.yaml.RoundTripConstructor
yaml_representer = ruamel.yaml.RoundTripRepresenter
yaml_tag = '!include'
def __init__(self, file):
self.file = file
@classmethod
def from_yaml(cls, loader, node):
return cls(loader.construct_scalar(node))
@classmethod
def to_yaml(cls, dumper, data):
if isinstance(data.file, ruamel.yaml.scalarstring.ScalarString):
style = data.file.style # ruamel.yaml>0.15.8
else:
style = None
return dumper.represent_scalar(cls.yaml_tag, data.file, style=style)
(无耻地抄袭自ruamel/_test/test_add_xxx.py
。)然后你就可以
code.insert(1, 'test', Include('test.yaml'), comment="new key")