Skip to content

Instantly share code, notes, and snippets.

@jstnlvns
Created February 12, 2018 18:40
Show Gist options
  • Save jstnlvns/10b752a2eb844c20557fec887132bbc7 to your computer and use it in GitHub Desktop.
Save jstnlvns/10b752a2eb844c20557fec887132bbc7 to your computer and use it in GitHub Desktop.
For use in Pastebot to convert CSV formatted text with headers to a markdown table.
#!/usr/bin/python
import sys
import csv
text = sys.stdin
text = text.readlines()
csvreader = csv.reader(text, delimiter=',', quotechar='"')
rowcnt = 0
for row in csvreader:
if rowcnt == 0:
num_cols = len(row)
outrow = '|'.join(row)
outformat = '|{}|'.format(outrow)
print outformat
head = '|-------' * num_cols
head = '{}|'.format(head)
print head
else:
outrow = '|'.join(row)
outformat = '|{}|'.format(outrow)
outformat.strip()
print outformat
rowcnt += 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment