summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBen Sima <ben@bensima.com>2025-05-01 11:46:16 -0400
committerBen Sima <ben@bensima.com>2025-05-01 11:46:40 -0400
commit168ebb4dc55d92c5c91a343e6790e75165b43cf6 (patch)
treebeb349c8e70a3c66457ec9f4b06bec19a5b4df1e
parentd4a9d5b0b3a3d26fb283c6a1318061bf97c90bbd (diff)
add csv2md script
-rwxr-xr-xcsv2md21
1 files changed, 21 insertions, 0 deletions
diff --git a/csv2md b/csv2md
new file mode 100755
index 0000000..ff91fad
--- /dev/null
+++ b/csv2md
@@ -0,0 +1,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)} |")