Convert YAML to CSV rows and columns
YAML is often used for data that is really a table: a team roster, a product catalog, a list of test cases or feature flags. This YAML to CSV converter turns such a list of records into CSV so you can sort it in a spreadsheet, import it into a database or share it with someone who does not work with YAML. Each record becomes a row and each field becomes a column.
Example
team: platform
members:
- name: Priya
role: lead
skills: [go, k8s]
address: {city: Austin, zip: "73301"}
- name: Liam
role: dev
skills: [js]
With the default options:
name,role,skills.0,skills.1,address.city,address.zip
Priya,lead,go,k8s,Austin,73301
Liam,dev,js,,,
How the converter finds the table
CSV is flat, so the converter first looks for the list of records inside your document. If the top level is a list, that list is used. If it is a mapping with exactly one list of records next to plain values, as with members above, the converter uses that list, and the scalar siblings such as team: platform are not part of the table. A mapping with no list at all becomes a single row, one column per key.
Columns are the union of all keys across records, in the order they first appear. A record that lacks a field simply gets an empty cell, which is why Liam's address columns are blank.
Flattening nested data
With Flatten nested objects on (the default), nested mappings become dot-notation columns such as address.city, and lists are expanded by position into skills.0, skills.1 and so on. This is the most spreadsheet-friendly layout and the one most import tools expect.
Turn flattening off to keep one column per top-level field. Nested values are then written as JSON text inside the cell:
name,role,skills,address
Priya,lead,"[""go"",""k8s""]","{""city"":""Austin"",""zip"":""73301""}"
Liam,dev,"[""js""]",
That form is useful when the CSV will be read by a program that parses the JSON column again, or when lists vary a lot in length.
Delimiter and quoting
- Delimiter – comma, semicolon, pipe or tab. Semicolons suit spreadsheet apps in locales that use a comma as the decimal separator.
- Quote all fields – wraps every value in double quotes. Without it, only values that contain the delimiter, quotes or line breaks are quoted, and embedded quotes are doubled (
say "hi"becomes"say ""hi"""). - Include header row – untick it when you append rows to an existing file.
Things to watch for
- Values keep the types YAML gave them. Unquoted
007is already the number 7 before it reaches the CSV, so quote codes and IDs in the YAML if leading zeros matter. - Spreadsheet apps may reformat values such as long numbers or date-like strings when they open a CSV. If you need exact control over the sheet, use YAML to Excel instead.
- Invalid YAML is reported with its line and column instead of producing a partial file.
Going the other way? CSV to YAML rebuilds YAML records from a CSV, and CSV viewer lets you check the output as a table.
Privacy
The conversion runs in your browser; the YAML is not uploaded. Only Load URL and Save & Share involve our server.
Convert YAML to CSV in Python
Load the YAML with PyYAML, then write the records with csv.DictWriter from the standard library. The step people miss is the column list: DictWriter raises ValueError when a record has a key that is not in fieldnames, so collect the keys of every record first. Records that lack a column get an empty cell. When you write to a file instead of sys.stdout, open it with newline="", because the csv module writes its own \r\n line endings.
# pip install pyyaml
import csv
import sys
import yaml
text = """
- name: Priya
role: lead
city: Austin
- name: Liam
role: dev
"""
rows = yaml.safe_load(text)
columns = list(dict.fromkeys(key for row in rows for key in row))
writer = csv.DictWriter(sys.stdout, fieldnames=columns)
writer.writeheader()
writer.writerows(rows)
The converter above returns the same three lines for this input, and it also flattens nested mappings and lists into dot-notation columns, which DictWriter does not do.