ruyaml YAML Parser

0.91.0 · active · verified Sat Apr 11

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

Install

Imports

Quickstart

This quickstart demonstrates how to load and dump YAML data while preserving comments, ordering, and block/flow styles using `ruyaml`'s default `YAML()` instance for round-trip parsing.

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.

view raw JSON →