Skip to content

Instantly share code, notes, and snippets.

@caiosba
Created October 16, 2015 16:30
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save caiosba/ca1309f1201609657f26 to your computer and use it in GitHub Desktop.
Save caiosba/ca1309f1201609657f26 to your computer and use it in GitHub Desktop.
Python script to convert from CSV to a pretty ASCii table
#!/usr/bin/python
from __future__ import print_function
import prettytable
import csv
import sys
def main(argv):
if len(sys.argv) != 3:
print('Usage: python csv2table.py [input file] [output]\n')
exit(1)
inputfile = sys.argv[1]
outputfile = sys.argv[2]
table = None
with open(inputfile, 'rb') as csvfile:
content = csv.reader(csvfile, delimiter=',', quotechar='"')
for row in content:
if table is None:
table = prettytable.PrettyTable(row)
else:
table.add_row(row)
output = open(outputfile, 'w')
print(table, file=output)
print('Done.\n')
if __name__ == "__main__":
main(sys.argv[1:])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment