Last active
November 12, 2026 15:42
-
-
Save panzi/19ecd0d606f97c07d2ea28cea0ac373a to your computer and use it in GitHub Desktop.
A very simple function to parse CSV or TSV data from Markdown fenced code blocks while ignoring everything else.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| import csv | |
| import sys | |
| from io import StringIO | |
| from typing import Iterable, Literal | |
| __all__ = 'get_tables', 'get_code_blocks', | |
| def _get_next_fence(s: str, index: int) -> tuple[int, str, str|None]|None: | |
| if index != 0 or not s.startswith('```'): | |
| index = s.find('\n```', max(index - 1, 0)) | |
| if index < 0: | |
| return None | |
| index += 1 | |
| lang_index = index + 3 | |
| lang_end_index = s.find('\n', lang_index) | |
| if lang_end_index < 0: | |
| return None | |
| lang = s[lang_index:lang_end_index] if lang_index != lang_end_index else None | |
| code_index = lang_end_index + 1 | |
| code_end_index = s.find('\n```\n', lang_end_index) | |
| if code_end_index < 0: | |
| if not s.endswith('\n```'): | |
| return None | |
| next_index = len(s) | |
| code_end_index = next_index - 3 | |
| else: | |
| code_end_index += 1 | |
| next_index = code_end_index + 4 | |
| code = s[code_index:code_end_index] | |
| return next_index, code, lang | |
| def get_code_blocks(s: str) -> Iterable[tuple[str, str|None]]: | |
| index = 0 | |
| n = len(s) | |
| while index < n: | |
| item = _get_next_fence(s, index) | |
| if item is None: | |
| break | |
| index, code, lang = item | |
| yield code, lang | |
| def get_tables(s: str, default_lang: Literal['csv']|Literal['tsv']='tsv') -> Iterable[list[list[str]]]: | |
| def_lang = default_lang.lower() | |
| for code, lang in get_code_blocks(s): | |
| if lang is None: | |
| lang = def_lang | |
| else: | |
| lang = lang.lower() | |
| match lang: | |
| case 'csv': | |
| dialect = 'excel' | |
| case 'tsv': | |
| dialect = 'excel-tab' | |
| case _: | |
| print(f'Unsupported table format {lang!r}, using TSV instead.', file=sys.stderr) | |
| dialect = 'excel-tab' | |
| data = list(csv.reader(StringIO(code), dialect=dialect)) | |
| yield data | |
| if __name__ == '__main__': | |
| import argparse | |
| from pprint import pprint | |
| ap = argparse.ArgumentParser() | |
| ap.add_argument('-f', '--fileformat', choices=['csv', 'tsv'], default='csv') | |
| ap.add_argument('files', nargs='*') | |
| args = ap.parse_args() | |
| fileformat = args.fileformat | |
| if args.files: | |
| for path in args.files: | |
| with open(path, 'r') as fp: | |
| s = fp.read() | |
| for table in get_tables(s, fileformat): | |
| pprint(table) | |
| else: | |
| import sys | |
| s = sys.stdin.read() | |
| for table in get_tables(s, fileformat): | |
| pprint(table) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment