Skip to content

Instantly share code, notes, and snippets.

@jfalava
Last active February 20, 2024 15:05
Show Gist options
  • Save jfalava/c28d4de9443d466e563fd1bab1cf03e4 to your computer and use it in GitHub Desktop.
Save jfalava/c28d4de9443d466e563fd1bab1cf03e4 to your computer and use it in GitHub Desktop.
Python script to convert CSV files to Markdown Tables

Note

This script requires this packages:

 pip3 install pandas tabulate
  • Change foo.csv to your CSV file name.
import pandas as pd
from tabulate import tabulate

def csv_to_markdown_table(filename):
    data = pd.read_csv(filename)

    resource_types = data['Resource Type'].unique()

    for resource_type in resource_types:
        filtered_data = data[data['Resource Type'] == resource_type]

        markdown_table = tabulate(filtered_data, headers="keys", tablefmt="pipe", showindex=False)

        output_filename = f"{resource_type.replace(' ', '_')}.md"

        with open(output_filename, "w") as f:
            f.write(markdown_table)

csv_to_markdown_table("foo.csv")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment