Created
July 24, 2014 12:21
-
-
Save maurice-audin/e8e1ade8d74dd2e5f903 to your computer and use it in GitHub Desktop.
CSV to pretty table
This file contains 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/env python | |
import prettytable | |
import sys | |
if len(sys.argv) != 2: | |
print "usage: tablify FILE.csv" | |
exit(1) | |
data = [] | |
with open(sys.argv[1]) as f: | |
s = f.readline() | |
while s != '': | |
data.append(s[:-1].split(';')) | |
s = f.readline() | |
if len(data) <= 1: | |
print "Error: csv file too short, should be at least two lines" | |
exit(1) | |
for d in data[1:]: | |
if len(d) != len(data[0]): | |
print "Error: Wrong number of values, should be " + str(len(data[0])) | |
print ";".join(d) | |
exit(1) | |
table = prettytable.PrettyTable(data[0]) | |
for h in data[0]: | |
table.align[h] = "l" | |
for d in data[1:]: | |
table.add_row(d) | |
print table |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment