Python ruamel不保留Yaml中换行符的格式

时间:2018-12-05 07:06:02

标签: python yaml newline indentation ruamel.yaml

我的原始Yaml文件在转储后要保留的破折号(-)后面包含换行符

#  This is my yaml
color_1:
    blue: 
        -
# I want to preserve this newline after the dash
            width_1: "1"
            height_1: [1]
            size_1: 4
        -
            width_1: "2"
            height_2: [1,2,3,4]
            size_2: 4

我的代码

import sys
from ruamel.yaml import YAML
yaml = YAML()
yaml.preserve_quotes = True
yaml.boolean_representation = ['False', 'True']
yaml.indent(mapping=4, sequence=3, offset=0)

def main():
    config_path = "my_yaml_before.yaml"
    config = yaml.load(open(config_path))
    yaml.dump(config, sys.stdout)

if __name__ == "__main__":
    main()

运行脚本后,我看到如下已删除换行符的转储文件,我还注意到列表项之间还有多余的空间,我也不想获得

#  This is my yaml
color_1:
    blue:
# I want to preserve this newline after the dash
    -  width_1: "1"
       height_1: [1]
       size_1: 4
    -  width_1: "2"
       height_2: [1, 2, 3, 4]
       size_2: 4

0 个答案:

没有答案