5
4

1 回答 1

3

该类ScalarString是 的基类LiteralScalarString,它没有您发现的代表。您应该只制作/保留这个 Python 字符串,因为它可以适当地处理特殊字符(引用需要引用以符合 YAML 规范的字符串)。

假设您有这样的输入:

- 1
- abc: |
    this is a short string scalar with a newline
    in it
- "there are also a multiline\nsequence element\nin this file\nand it is longer"

您可能想要执行以下操作:

import ruamel.yaml
from ruamel.yaml.scalarstring import LiteralScalarString, preserve_literal


def walk_tree(base):
    from ruamel.yaml.compat import string_types

    def test_wrap(v):
        v = v.replace('\r\n', '\n').replace('\r', '\n').strip()
        return v if len(v) < 72 else preserve_literal(v)

    if isinstance(base, dict):
        for k in base:
            v = base[k]
            if isinstance(v, string_types) and '\n' in v:
                base[k] = test_wrap(v)
            else:
                walk_tree(v)
    elif isinstance(base, list):
        for idx, elem in enumerate(base):
            if isinstance(elem, string_types) and '\n' in elem:
                base[idx] = test_wrap(elem)
            else:
                walk_tree(elem)

yaml = YAML()

with open("input.yaml", "r") as fi:
    data = yaml.load(fi)

walk_tree(data)

with open("output.yaml", "w") as fo:
    yaml.dump(data, fo)

获得输出:

- 1
- abc: "this is a short string scalar with a newline\nin it"
- |-
  there are also a multiline
  sequence element
  in this file
  and it is longer

一些注意事项:

  • LiteralScalarString优先使用PreservedScalarString. 后一个名称是从它是唯一保留的字符串类型时的残余。
  • 您可能没有包含字符串的序列元素,因为您没有 import preserve_literal,尽管它仍在复制的代码中使用。
  • 我将“包装”代码分解为 test_wrap,由值和元素包装使用,最大行长度设置为 72 个字符。
  • data[1]['abc']加载为LiteralScalarString。如果要保留现有的文字样式字符串标量,则应在测试 type 之前测试这些标量string_types
  • 我将新 API 与一个实例一起使用YAML()
  • width如果您将示例中的 72 增加到默认值 80 以上,您可能必须将该属性设置为1000 之类的值,以防止自动换行。( yaml.width = 1000
于 2016-12-28T07:10:34.440 回答