python use Pyyaml and keep format

后端 未结 3 1639
隐瞒了意图╮
隐瞒了意图╮ 2021-01-18 12:56

Here is a config file, I use PyYAML to change some value from it and then I write some config, but it will change my format, it confuses me.

 $ results.yaml          


        
3条回答
  •  别那么骄傲
    2021-01-18 13:28

    ruamel.yaml unfortunately does not completely preserve original format, quoting its docs:

    Although individual indentation of lines is not preserved, you can specify separate indentation levels for mappings and sequences (counting for sequences does not include the dash for a sequence element) and specific offset of block sequence dashes within that indentation.

    I do not know any Python library that does that.

    When I need to change a YAML file without touching its format I reluctantly use regexp (reluctantly as it's almost as bad as parsing XHTML with it).

    Please feel free to suggest a better solution if you know any, I would gladly learn about it!

提交回复
热议问题