
Reading YAML Front Matter From Markdown Files in Python
Split a Markdown file into YAML metadata and body with one regex, then handle dates, CRLF, a BOM, a missing closing line and body text with dashes.
Config files that look friendly until the indentation bites. Parsing it, linting it and avoiding its type surprises.

Split a Markdown file into YAML metadata and body with one regex, then handle dates, CRLF, a BOM, a missing closing line and body text with dashes.

dict update is shallow and replaces whole blocks. A 10-line deep merge, the list policy choice and what null means, tested on a base and an override file.

diff -w hides YAML indentation changes that alter meaning. Normalize both files with yq sort_keys and compare as data to ignore layout and order only.

Deep-merge a base and an override with yq eval-all, choose whether lists replace or append, and check what a null or an empty value in the override does.

Load YAML, validate it with pydantic models and get every error with its path. The Norway problem caught, extra keys rejected and ranges enforced.

A YAML file can hold several documents separated by three dashes. safe_load fails on them, safe_load_all reads them and safe_dump_all writes them.

Edit a YAML file from Python and keep its comments, key order, quotes and block strings with ruamel.yaml round-trip mode, plus settings and caveats.

Convert YAML to JSON with PyYAML and ruamel.yaml, and handle dates, yes and no, octal numbers, non-string keys, anchors, multiple documents and floats.

Convert JSON to YAML with yq, Python's yq and a PyYAML one-liner, and see which strings get corrupted by YAML 1.1 readers: NO, yes, 12:30, 0123 and 1e3.

PyYAML safe_dump sorts keys, escapes Unicode and quotes multiline strings with blank lines. The options and a block-string representer that fix each default.

What yaml.safe_load returns, how to open files correctly, why the C loader is six times faster and the type surprises that come with PyYAML.

A messy file with nine findings, reported by yamllint line by line, then repaired with yq and a project config. The workflow that finds and fixes indentation.
We send one email when a new article is published. No spam.