pyaml - Pretty YAML Dumper
pyaml is a PyYAML-based module designed to produce more human-readable and aesthetically pleasing YAML-serialized data. It provides enhanced defaults for indentation, key sorting, and block-style formatting. The current version is 26.2.1, and it maintains a moderate release cadence, with updates typically several times a year to reflect PyYAML changes or minor enhancements.
Warnings
- gotcha pyaml focuses exclusively on *dumping* (serialization) YAML data. It does not provide `load` or `loads` functionality for deserializing YAML. For deserialization, you must use `PyYAML` directly (e.g., `yaml.safe_load`).
- gotcha While `pyaml` is built on `PyYAML`, its default output behavior (e.g., `sort_keys=True`, specific indentation, preference for block style) differs significantly from `PyYAML`'s raw `dump` defaults. This is its core purpose, but users expecting a drop-in replacement that only visually enhances existing `PyYAML` output might be surprised by structural changes.
- gotcha pyaml requires `PyYAML` to be installed. Although `pip install pyaml` typically installs `PyYAML` as a dependency, it's essential to understand that `pyaml` is a wrapper, not a standalone YAML library. Issues with `PyYAML` (e.g., version conflicts, specific platform issues) can affect `pyaml`.
Install
-
pip install pyaml
Imports
- dump
import pyaml # then use pyaml.dump(...)
- dumps
import pyaml # then use pyaml.dumps(...)
Quickstart
import pyaml
import sys
data = {
'name': 'Alice',
'age': 30,
'isStudent': False,
'courses': [
{'title': 'Math', 'credits': 3},
{'title': 'History', 'credits': 4}
],
'address': {
'street': '123 Main St',
'city': 'Anytown',
'zip': '12345'
}
}
# Dump to stdout with pretty formatting
print('--- Pretty YAML Output (to stdout) ---')
pyaml.dump(data, sys.stdout)
# Or get as a string
yaml_string = pyaml.dumps(data)
print('\n--- Pretty YAML Output (as string) ---')
print(yaml_string)