YAML-在新文档中保留文本格式

YAML-在新文档中保留文本格式,yaml,ruamel.yaml,Yaml,Ruamel.yaml,我所拥有的: a: some meta info b: more meta info c: actual nicely formatted text that has line breaks 我希望通过使用文档分隔符将c移动到新的YAML文档--- 但当我使用第二种选择时,我会丢失格式,比如新行等 有没有办法使用后一种YAML方法格式并保持换行 我目前正在使用ruamel.yaml库来读取这个yaml和下面的函数来加载我的文件 yaml.load_all(f,Loader=yam

我所拥有的:

a: some meta info
b: more meta info
c: actual nicely
   formatted text that
   has line breaks 
我希望通过使用文档分隔符将
c
移动到新的YAML文档---

但当我使用第二种选择时,我会丢失格式,比如新行等

有没有办法使用后一种YAML方法格式并保持换行

我目前正在使用ruamel.yaml库来读取这个yaml和下面的函数来加载我的文件

yaml.load_all(f,Loader=yaml.Loader)


如果希望换行符位于加载的值中,我建议将第二个文档设置为文字样式标量

如果您有
input.yaml

a: some meta info
b: more meta info
--- |
actual nicely
formatted text that
has line breaks
那么这个程序,

from pathlib import Path
import ruamel.yaml

path_name = Path('input.yaml')


yaml = ruamel.yaml.YAML()
for data in yaml.load_all(path_name):
    print(repr(data))
给出:

ordereddict([('a', 'some meta info'), ('b', 'more meta info')])
'actual nicely\nformatted text that\nhas line breaks\n'
请注意,一些YAML库(错误地)假设文档根级别的文字样式标量需要缩进

ordereddict([('a', 'some meta info'), ('b', 'more meta info')])
'actual nicely\nformatted text that\nhas line breaks\n'