-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCSVFormat.py
More file actions
31 lines (25 loc) · 831 Bytes
/
CSVFormat.py
File metadata and controls
31 lines (25 loc) · 831 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# This can be used to convert a normal csv file into a beautiful tabular form in ASCII Art style.
import csv
import sys
from tabulate import tabulate
def main():
if len(sys.argv) < 2:
sys.exit("Too few command-line arguments")
elif len(sys.argv) > 2:
sys.exit("Too many command-line arguments")
else:
if sys.argv[1][-4:] != ".csv":
sys.exit("Not a CSV file")
else:
print(tabulize(sys.argv[1]))
def tabulize(file):
try:
with open(file) as f:
reader = csv.reader(f)
table = tabulate(reader, headers="firstrow", tablefmt="grid")
return table
except FileNotFoundError:
sys.exit("File does not exist")
if __name__ == "__main__":
main()
#! Run using `python CSVFormat.py nameOfFile.csv` in terminal