python yaml.dump bad indentation

前端 未结 2 1442
眼角桃花
眼角桃花 2021-02-05 03:27

I\'m executing the following python code:

import yaml


foo = {
    \'name\': \'foo\',
    \'my_list\': [{\'foo\': \'test\', \'bar\': \'test2\'}, {\'foo\': \'tes         


        
2条回答
  •  逝去的感伤
    2021-02-05 04:02

    Your output, as shown, is incomplete as print(yaml.dump()) gives you an extra empty line after name: foo. It is also slower and uses more memory than directly streaming to sys.stdout.

    You are probably using PyYAML and, apart from only supporting the outdated YAML 1.1 specification, it is very limited in control over the dumped YAML.

    I suggest you use ruamel.yaml (disclaimer: I am the author of that package), where you can specify identation separately for mappings and sequences and also indicate how far to offset the dash within the indent before the sequence element:

    import sys
    import ruamel.yaml
    
    foo = {
        'name': 'foo',
        'my_list': [{'foo': 'test', 'bar': 'test2'}, {'foo': 'test3', 'bar': 'test4'}],
        'hello': 'world'
    }
    
    
    yaml = ruamel.yaml.YAML()
    yaml.indent(sequence=4, offset=2)
    yaml.dump(foo, sys.stdout)
    

    which gives:

    name: foo
    my_list:
      - foo: test
        bar: test2
      - foo: test3
        bar: test4
    hello: world
    

    Please note that the order of the keys is implementation dependent (but can be controlled, as ruamel.yaml can round-trip the above without changes).

提交回复
热议问题