yaml.dump appends a line at the end of the file
See original GitHub issueHello,
yaml.dump()
adds a new line at the end of a file. here an example to illustrate my problem.
test.yaml
foo:
bar: hello
loul: world
No new line after loul: world
from pathlib import Path
import yaml
def read_text():
with open("test.yaml") as file:
data = yaml.safe_load(file)
with open("test2.yaml", mode="w") as file:
yaml.safe_dump(data, file, default_flow_style=False, sort_keys=False)
file_text = Path("test.yaml").read_text()
new_file_text = Path("test2.yaml").read_text()
assert file_text == new_file_text
# Raise AssertionError
if __name__ == '__main__':
read_text()
test2.yaml
foo:
bar: hello
loul: world
# this line is added
Is this correct ?
Issue Analytics
- State:
- Created 4 years ago
- Comments:9 (5 by maintainers)
Top Results From Across the Web
How to indent lines when appending to existing yaml file with ...
Instead instantiate a YAML instance and use its dump method. The default ( round-trip parser) uses block-style for new elements yaml = ruamel....
Read more >yaml.dump() appending to file rather than overwriting
yaml.dump() appending to file rather than overwriting how do i make set 'hi' to 1 and 'hello' to 1 import yaml class myClass():...
Read more >Examples — Python YAML package documentation
Basic round trip of parsing YAML to Python objects, modifying and generating YAML: ... newline at the end # print(ruamel.yaml.dump(code, Dumper=ruamel.yaml.
Read more >PyYAML Documentation
yaml.dump supports a number of keyword arguments that specify formatting details for the emitter. For instance, you may set the preferred intendation and...
Read more >YAML: The Missing Battery in Python
Choose the Dumper Class; Dump to a String, a File, or a Stream ... pure Python dumper appends optional dots at the end...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
YAML files with missing final linebreaks are actually valid. It’s just that PyYAML does not keep this information (or any other spaces or comments) when loading.
Is there a way to raise an error at load time to verify if the original file is no valid from yaml point of view?