
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.
Tag
134 posts

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.

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.

Pull the title with inline markup, authors with ORCID and affiliations, group authors, editors and the several dates out of one JATS article front matter.

Stream XML into Arrow batches with lxml, query them in DuckDB at once, or try the community webbed extension. 1.2 million lines in 7.9 s with a 53 ms query.

Read a package metadata document where version numbers are keys: json_each, DuckDB MAP inference, semver sorting, dist-tags versus max, and the time map.

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.

A product export writes names with commas and no quotes, so rows have 6 fields, not 4. Rejoin the middle fields from the good ends and verify every row.

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.