ruamel.yaml.string is a plugin for the `ruamel.yaml` YAML parser/emitter library. It extends the `ruamel.yaml.YAML` instance by adding `dump_to_string` (and its alias `dumps`) methods, allowing users to serialize YAML documents directly into a Python string instead of writing to a file-like object. The current version is 0.1.1, last updated in May 2023, and its release cadence is tied to `ruamel.yaml` and bug fixes.
pip install ruamel.yaml.stringVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to initialize `ruamel.yaml.YAML` with `typ=['rt', 'string']` to enable the `dump_to_string` and `dumps` methods, which return the YAML document as a Python string. It also shows the `add_final_eol` parameter for controlling the trailing newline behavior.
To add a final newline, use `yaml.dump_to_string(data, add_final_eol=True)` or `yaml.dumps(data, add_final_eol=True)`.
Prefer `yaml.dump(data, output_stream)` for performance-critical scenarios where the output stream is known.
Ensure your `YAML` instance is created as `yaml = YAML(typ=['rt', 'string'])` to enable the string dumping functionality.
Initialize your YAML object with `yaml = YAML(typ=['rt', 'string'])`.
If you want a string, use `yaml_string = yaml.dumps(data)`. If you want to dump to a file, use `yaml.dump(data, file_handle)`.