I know there are a few questions about this on SO, but I couldn't find what I was looking for.
I'm using pyyaml to read (.load()) a .yml file, modify or add a key, and then write it (.dump()) again. The problem is that I want to keep the file format post-dump, but it changes.
For example, I edit the key en.test.index.few to say "Bye" instead of "Hello"
Python:
with open(path, 'r', encoding = "utf-8") as yaml_file: self.dict = pyyaml.load(yaml_file) Then, afther changing the key:
with open(path, 'w', encoding = "utf-8") as yaml_file: dump = pyyaml.dump(self.dict, default_flow_style = False, allow_unicode = True, encoding = None) yaml_file.write( dump ) Yaml:
Before:
en: test: new: "Bye" index: few: "Hello" anothertest: "Something" After:
en: anothertest: Something test: index: few: Hello new: Bye Is there a way to keep the same format?, for example the qoutes and order. Am I using the wrong tool for this?
I know maybe the original file it's not entirely correct, but I have no control over it (it's a Ruby on Rails i18n file).
Thank you very much.
YAML is a data serialization format designed for human readability and interaction with scripting languages. PyYAML is a YAML parser and emitter for Python. PyYAML features a complete YAML 1.1 parser, Unicode support, pickle support, capable extension API, and sensible error messages.
In this case, yaml. dump will write the produced YAML document into the file. Otherwise, yaml. dump returns the produced document.
YAML (YAML Ain't Markup Language) is a human-readable data-serialization language. It is commonly used for configuration files, but it is also used in data storage (e.g. debugging output) or transmission (e.g. document headers).
Below, ruamel.yaml is used instead.
ruamel.yaml is actively maintained. Unlike PyYAML, ruamel.yaml supports:
yaml.dump() to dump a dictionary loaded by a prior call to yaml.load(): ruamel.yaml cleverly respects all input formatting. Everything. The whole stylistic enchilada. The entire literary shebang. All. Switching from PyYAML to ruamel.yaml in existing applications is typically as simple as changing the library import to:
from ruamel import yaml This works because ruamel.yaml is a PyYAML fork that conforms to the PyYAML API.
No other changes should be needed. The yaml.load() and yaml.dump() functions should continue to behave as expected.
For backward compatibility with PyYaml, the yaml.load() and yaml.dump() functions do not perform roundtrip preservation by default. To do so, explicitly pass:
Loader=ruamel.yaml.RoundTripLoader keyword parameter to yaml.load().Dumper=ruamel.yaml.RoundTripDumper keyword parameter to yaml.dump().An example kindly "borrowed" from ruamel.yaml documentation:
import ruamel.yaml inp = """\ # example name: # Yet another Great Duke of Hell. He's not so bad, really. family: TheMighty given: Ashtaroth """ code = ruamel.yaml.load(inp, Loader=ruamel.yaml.RoundTripLoader) code['name']['given'] = 'Astarte' # Oh no you didn't. print(ruamel.yaml.dump(code, Dumper=ruamel.yaml.RoundTripDumper), end='') It is done. Comments, ordering, quoting, and whitespace will now be preserved intact.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With