This is my valid YAML file:
# my_yaml.yaml
- aaa: zzzzz
bbbb: 'lksdjflksjdflksdj'
xxxx:
qqqq:
- sldflsdkjflks
fffff: []
- aaa: zzzzz
bbbb: 'lksdjflksjdflksdj'
xxxx:
qqqq:
- sldflsdkjflks
fffff: []
- aaa: zzzzz
bbbb: 'lksdjflksjdflksdj'
xxxx:
qqqq:
- sldflsdkjflks
fffff: []
But this is what ruamel.yaml
generates:
from ruamel.yaml import YAML
yaml = YAML()
yaml.preserve_quotes = True
yaml.default_flow_style = None
yaml.explicit_start = False
formatted_config = yaml.load(Path("my_yaml.yaml").open().read())
yaml.dump_all(formatted_config, Path("/tmp/derps.yaml"))
/tmp/derps.yaml
looks like:
aaa: zzzzz
bbbb: 'lksdjflksjdflksdj'
xxxx:
qqqq:
- sldflsdkjflks
fffff: []
---
aaa: zzzzz
bbbb: 'lksdjflksjdflksdj'
xxxx:
qqqq:
- sldflsdkjflks
fffff: []
---
aaa: zzzzz
bbbb: 'lksdjflksjdflksdj'
xxxx:
qqqq:
- sldflsdkjflks
fffff: []
I don't understand why it's inserting ---
for each list item. All valid YAML parsers just see this as a list.
How can I make ruamel process this correctly?
Those ---
separators indicate multiple documents embedded in a single file.
Instead of yaml.dump_all
, which accepts a list of documents, you should be using yaml.dump
which accepts a single document:
from ruamel.yaml import YAML
from pathlib import Path
yaml = YAML()
yaml.preserve_quotes = True
yaml.default_flow_style = None
yaml.explicit_start = False
formatted_config = yaml.load(Path("my_yaml.yaml").open().read())
yaml.dump(formatted_config, Path("/tmp/derps.yaml"))