Read and write YAML 1.2 in Python
Actively maintained. Last release 2 days ago, commits in 10 of the last 26 weeks. A safe choice.
pip install astral-yamlREADME as published on PyPI with version 0.0.8.
astral-yaml for Python
astral_yaml reads and writes YAML 1.2 on CPython 3.11 and newer.
Reading YAML
The loader uses the YAML 1.2 core schema. In this configuration, the port
numbers become Python integers and the unquoted word on stays a string.
import astral_yaml as yaml
config = yaml.loads("ports: [80, 443]\nmode: on")
assert config == {"ports": [80, 443], "mode": "on"}
load and loads accept at most one document per call. Empty or
comment-only input returns None. A second document raises yaml.YAMLError.
Use load_all for an open text or binary file and loads_all for a string
or encoded bytes. Both return a list in document order. An empty or
comment-only stream gives [], and an explicit empty document contributes
None. An error in any document raises during the call.
configs = yaml.loads_all("---\nmode: on\n---\nports: [80, 443]\n")
assert configs == [{"mode": "on"}, {"ports": [80, 443]}]
Mapping keys must be scalars and must stay unique when converted to Python.
For example, {true: first, 1: second} raises yaml.YAMLError because
True == 1 in Python.
Writing YAML
dumps returns YAML as a string, and dump writes it to a text stream.
Both use block style by default, with two spaces per indentation level.
Pass style="flow" for collections written with brackets and braces.
Block mappings use plain string keys when their spelling and type are unambiguous.
String values and flow-style string keys use double quotes.
Empty collections use [] or {} in either style.
config["ports"].append(8080)
assert yaml.dumps(config) == 'ports:\n - 80\n - 443\n - 8080\nmode: "on"\n'
assert yaml.dumps(config, style="flow") == '{"ports": [80, 443, 8080], "mode": "on"}\n'
with open("config.yaml", "w", encoding="utf-8") as file:
yaml.dump(config, file)
# In binary mode, load detects UTF-8, UTF-16, or UTF-32.
with open("config.yaml", "rb") as file:
assert yaml.load(file) == config
Tuples serialize as sequences and load back as lists. If two dictionary entries share a list, the dumper uses a YAML alias so the entries still share a list when you read them back. Aliases also preserve cycles.