ruyaml YAML Parser
ruyaml is a fork of ruamel.yaml, a YAML parser and emitter for Python that aims to preserve comments, order, and other structural details during round-trip loading and dumping. It is actively maintained, with version 0.91.0 being the latest stable release, and receives updates periodically, often merging upstream changes from `ruamel.yaml`.
Warnings
- breaking The package was renamed from `ruamel.yaml` to `ruyaml` starting with version 0.20.0. All import paths and installation commands must reflect this change.
- gotcha By default, `ruyaml` uses a round-trip aware loader/dumper (`YAML()`) which preserves comments, ordering, and tag information. Users migrating from `PyYAML` might expect simpler type conversion or different default formatting.
- gotcha When dumping, `ruyaml` might automatically quote strings containing special characters or represent certain data structures (like dicts or lists) in block style by default, which can differ from `PyYAML`'s output or simple string representations.
Install
-
pip install ruyaml
Imports
- YAML
from ruyaml import YAML
Quickstart
from ruyaml import YAML
from io import StringIO
# Initialize a YAML instance for round-trip parsing
yaml = YAML()
# Example YAML data with comments and specific ordering
data = """
# User Configuration
name: John Doe
age: 30 # Current age
cities:
- New York
- London
options: {enabled: true, max_retries: 5}
"""
# Load YAML data from a string
stream = StringIO(data)
cfg = yaml.load(stream)
# Modify data - comments and structure are preserved
cfg['age'] = 31
cfg['cities'].append('Paris')
cfg['options']['timeout'] = 30 # Adding a new key
# Dump the modified YAML data back to a string, preserving original formatting
output_stream = StringIO()
yaml.dump(cfg, output_stream)
print(output_stream.getvalue())
# Expected output will include comments and maintain much of the original layout.