且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用制表符缩进的 YAML 配置

更新时间:2023-10-03 12:39:34

虽然制表符在 YAML 中有效,但它们不能用于缩进,在当前版本中都没有(1.2,也不在较旧的 1.1,或1.0)

Although tab characters are valid in YAML, they cannot be used in indentation, in neither the current version (1.2, nor in the older 1.1, or 1.0)

意味着不能在行首出现制表符,如下所示示例显示

That does not imply a tab cannot occur at the start of line, as the following example shows

import sys
import ruamel.yaml

yaml_str = """\
'xxx
\tyyy'
"""

yaml = ruamel.yaml.YAML()
yaml.explicit_start = True
data = yaml.load(yaml_str)
print(data)

运行没有错误并给出:

xxx yyy

如果您从 yaml_str 中删除单引号,您将得到你得到的错误(在第 2 行,第 1 列),因为解析器必须考虑 yyy 是否开始一个新的令牌(同时扫描单个引用标量它不这样做).

if you remove the single quotes from the yaml_str, you will however get the error that you got (on line 2, column 1), because the parser has to consider if yyy starts a new token (while scanning the single quoted scalar it doesn't do that).

没有看到实际的 YAML,很难断言,但是可能你的工具是罪魁祸首.你可能会逃脱更换标签:

Without seeing the actual YAML, it is difficult to say defitively, but probalby your tool is to blame. You might get away with replacing the tabs:

with open('yourfile.yaml') as fp:
    data = yaml.load(fp.read().replace('\t', ' '))