为什么 PyYAML 和 ruamel.yaml 在单引号时转义特殊字符?

Why are PyYAML and ruamel.yaml escaping special characters when single quoted?

我有一个 YAML 文件,想限制某个字段不包含空格。

这是一个演示我的尝试的脚本:

test.py

#!/usr/bin/env python3

import os
from ruamel import yaml

def read_conf(path_to_config):
    if os.path.exists(path_to_config):
        conf = open(path_to_config).read()
        return yaml.load(conf)
    return None

if __name__ == "__main__":
    settings = read_conf("hello.yaml")
    print("type of name: {0}, repr of name: {1}".format(type(
             settings['foo']['name']), repr(settings['foo']['name'])))
    if any(c.isspace() for c in settings['foo']['name']):
        raise Exception("No whitespace allowed in name!")

这是我第一次剪切 YAML 文件:

hello.yaml

foo:
    name: "hello\t"

在上面的 YAML 文件中,正确引发了异常:

type of name: <class 'str'>, repr of name: 'hello\t'
Traceback (most recent call last):
  File "./test.py", line 16, in <module>
    raise Exception("No whitespace allowed in name!")
Exception: No whitespace allowed in name!

但是,如果我将双引号更改为单引号,则不会出现异常:

08:23 $ ./test.py 
type of name: <class 'str'>, repr of name: 'hello\t'

使用 ruamel.yaml==0.11.11PyYAML=3.11 时都会出现此行为。

为什么这些 Python YAML 解析器中的单引号和双引号之间存在差异,而据我所知,它们在 YAML 规范中没有功能差异?如何防止特殊字符被转义?

单引号字符串和双引号字符串在 YAML 规范中存在巨大差异。在 single quoted scalars 中你只能转义单引号:

The single-quoted style is specified by surrounding “'” indicators. Therefore, within a single-quoted scalar, such characters need to be repeated. This is the only form of escaping performed in single-quoted scalars.

因此 'hello\t' 中的 \ 没有特殊功能,该标量由字母 hel (2x)、o\t

反斜杠转义仅在双引号 YAML 标量中受支持。