Skip to content

Instantly share code, notes, and snippets.

@maurice-audin
Created July 24, 2014 12:21
Show Gist options
  • Save maurice-audin/e8e1ade8d74dd2e5f903 to your computer and use it in GitHub Desktop.
Save maurice-audin/e8e1ade8d74dd2e5f903 to your computer and use it in GitHub Desktop.
CSV to pretty table
#! /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