Skip to content

Instantly share code, notes, and snippets.

@guyarad
Forked from caiosba/csv2table.py
Created August 4, 2020 07:18
Show Gist options
  • Save guyarad/e9e6941443173ad4e6147e212d0c156f to your computer and use it in GitHub Desktop.
Save guyarad/e9e6941443173ad4e6147e212d0c156f 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