Convert a Markdown table to CSV
Goal: Turn a table in a doc into CSV, e.g. to pull it into a spreadsheet.
Prerequisites: The table module, via -A or nodes.
Query
$ mq -A 'import "table" | table::tables | first | table::to_csv' README.md
Input
| Name | Age |
| ----- | --- |
| Alice | 30 |
Output
Name,Age
Alice,30
Using a different delimiter
to_csv takes an optional delimiter, so the same function produces TSV or PSV output too:
$ mq -A 'import "table" | table::tables | first | table::to_csv(self, "\t")' README.md
Name Age
Alice 30
Notes
- Fields containing the delimiter, a quote, or a newline are quoted and escaped per RFC 4180 automatically. A cell holding
"Hi, there"becomes"""Hi, there"""in the output, no manual escaping needed. - A document with more than one table?
table::tablesreturns all of them as an array; pick the one you want withfirst, or by index with(table::tables())[1]for the second table. - Want structured data instead of a CSV string, e.g. to feed into
join_byor another array builtin? Usetable::to_arrayinstead, which returns an array of dicts keyed by header text. - Going the other direction? See Convert CSV to a Markdown table.