ruamel.yaml
ruamel.yaml is a YAML parser and emitter for Python that supports round-trip preservation of comments, sequence and mapping flow styles, and map key order. The current version is 0.19.1, released on March 28, 2026. It is actively maintained with regular updates.
Warnings
- breaking The 'load' method in ruamel.yaml requires a file object, not a string. Ensure you open the file before passing it to 'load'.
- gotcha The 'YAML' class is case-sensitive. Using 'yaml' instead of 'YAML' will result in an AttributeError.
Install
-
pip install ruamel.yaml
Imports
- YAML
import ruamel.yaml as YAML
Quickstart
import os
import ruamel.yaml as YAML
# Load YAML data
with open(os.environ.get('YAML_FILE_PATH', 'data.yaml'), 'r') as file:
data = YAML().load(file)
# Modify data
data['new_key'] = 'new_value'
# Save YAML data
with open(os.environ.get('YAML_FILE_PATH', 'data.yaml'), 'w') as file:
YAML().dump(data, file)