使用ruamel.yaml和preserve_quotes = True时,是否有一种方法可以强制双向yaml使用单引号?

时间:2019-06-13 20:48:58

标签: ruamel.yaml

[更新后的问题,preserve_quotes可以正常工作]

无论输入中使用哪种类型的引号,如何强制输出使用单引号,但仅在使用引号的情况下?

注意:使用ruamel.yaml =>

的项目的(原始)源代码

1 个答案:

答案 0 :(得分:0)

您的示例输出中的单引号已更改,即未保留 一个最小的例子说明了事情并非如此。

import sys
import ruamel.yaml

yaml_str = """---
metadata:
  annotations:
    first: '1'
    second: any string
"""

yaml = ruamel.yaml.YAML(typ='rt')
data = list(yaml.load_all(yaml_str))
yaml.preserve_quotes = True
yaml.explicit_start = True
yaml.dump_all(data, sys.stdout)

给出:

---
metadata:
  annotations:
    first: '1'
    second: any string

使用您的format_yaml例程:

import sys
import pathlib
import ruamel.yaml
YAML = ruamel.yaml.YAML

yaml_file = pathlib.Path("temp.yaml")
yaml_file.write_text("""---
metadata:
  annotations:
    first: '1'
    second: any string
""")


def format_yaml(input_file,
                output_file,
                explicit_start=True,
                explicit_end=False,
                default_flow_style=False,
                dash_inwards=True,
                quotes_preserved=True,
                parsing_mode='rt'):
    """
    Load a file and save it formated :
    :param input_file: the input file
    :param output_file: the output file
    :param explicit_start: write the start of the yaml doc even when there is \
                            only one done in the file
    :param default_flow_style: if False, block style will be used for nested \
                              arrays/maps
    :param dash_inwards: push dash inwards if True
    :param quotes_preserved: preserve quotes if True
    :param parsing_typ: safe or roundtrip (rt) more
    """
    yaml = YAML(typ=parsing_mode)
    yaml.explicit_start = explicit_start
    yaml.explicit_end = explicit_end
    yaml.default_flow_style = default_flow_style
    yaml.preserve_quotes = quotes_preserved
    if dash_inwards:
        yaml.indent(mapping=2, sequence=4, offset=2)

    if input_file is not None:
        with open(input_file, 'rt') as f_input:
            parsed = yaml.load_all(f_input.read())
    else:
        parsed = yaml.load_all(sys.stdin.read())
    ready_for_dump = []
    try:
        # Read the parsed content to force the scanner to issue errors if any
        for data in parsed:
            ready_for_dump.append(data)
    except ScannerError as e:
        print("Something is wrong in the input file, got error from Scanner")
        print(e)
        return
    if output_file is not None:
        with open(output_file, 'wt') as out:
            yaml.dump_all(ready_for_dump, out)
    else:
        yaml.dump_all(ready_for_dump, sys.stdout)

format_yaml(yaml_file, None)

这还提供:

---
metadata:
  annotations:
    first: '1'
    second: any string

,如果在输入中使用双引号,则在输出中将获得双引号。 因此,请提供一个最小完整示例程序,以显示该行为 您所获得的信息,以及ruamel.yaml和python版本以及您在其上进行过测试的平台(上面在Linux 3.7.3上使用0.15.97进行了测试,在Linux上使用python 2.7.15(已安装pathlib2进行了测试) )。


BTW,更改所有单引号字符串以转储为双引号字符串的最简单方法 往返模式中的方法是通过重写represent_single_quoted_scalarstring方法 (例如,通过为其分配一种yaml.representer方法)。