summaryrefslogtreecommitdiff
path: root/csv2md
blob: ff91fad631db49347c8be076e059a18237aad0fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#!/usr/bin/env python3
# csv2md - Convert CSV to Markdown table
import csv
import fileinput
import sys

# Read from stdin or file specified as argument
csv_data = ''.join(line for line in fileinput.input())
reader = csv.reader(csv_data.splitlines())
rows = list(reader)

if rows:
    # Print header row
    print(f"| {' | '.join(rows[0])} |")

    # Print separator row
    print(f"| {' | '.join(['---'] * len(rows[0]))} |")

    # Print data rows
    for row in rows[1:]:
        print(f"| {' | '.join(row)} |")