直接看效果(pip install pyyaml):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import yaml

yaml_data = """
example1: |
This is line one.
This is line two.
example2: |-
This is line one.
This is line two.
example3: >-
This is line one.
This is line two.
"""

# 将 YAML 数据解析为 Python 对象
data = yaml.safe_load(yaml_data)

# 访问特定字段
print(f"example1: {data['example1']}")
print(f"example2: {data['example2']}")
print(f"example3: {data['example3']}")

输出结果:

1
2
3
4
5
6
example1: This is line one.
This is line two.

example2: This is line one.
This is line two.
example3: This is line one. This is line two.

结论

在 YAML 中,||->- 都用于定义多行字符串,但它们的处理方式不同:

  1. |

    • 保留换行符。
    • 生成的字符串在每行的末尾保留换行符。
  2. |-

    • 类似于 |,但去掉最后的换行符。
    • 结果中的最后一行不会有换行符。
  3. >-

    • 将换行符转换为空格。
    • 结果中的所有行会被连接成一行,中间用空格分隔,最后一行也没有换行符。