Created
February 12, 2018 18:40
-
-
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.
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/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