-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcsv2table.py
executable file
·77 lines (61 loc) · 1.64 KB
/
csv2table.py
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/env python3
# SPDX-License-Identifier: WTFPL
# /// script
# dependencies = ["prettytable"]
# ///
from argparse import ArgumentParser
import csv
import sys
from prettytable import PrettyTable, TableStyle
def sniff(fn):
with open(fn) as fd:
sn = csv.Sniffer()
return sn.sniff(fd.read(1024))
def read_rows(fd, args):
if args.header:
for row in csv.DictReader(fd, delimiter=args.delimiter):
yield row
else:
for row in csv.reader(fd, delimiter=args.delimiter):
yield {str(n): v for n, v in enumerate(row)}
def main():
parser = ArgumentParser()
parser.add_argument('-H', '--header', action='store_true')
parser.add_argument('-d', '--delimiter', default=',')
parser.add_argument(
'-b', '--box', action='store_true',
help='Use Unicode pretty characters instead of plain ASCII table',
)
parser.add_argument(
'--markdown', action='store_true',
help='Output markdown table',
)
parser.add_argument(
"--html", action="store_true",
help="Output HTML table",
)
parser.add_argument('--sniff', action='store_true')
parser.add_argument('file', nargs='?', default='-')
args = parser.parse_args()
if args.file == "-":
fd = sys.stdin
else:
fd = open(args.file)
with fd:
data = list(read_rows(fd, args))
table = PrettyTable()
if args.box:
table.set_style(TableStyle.SINGLE_BORDER)
elif args.markdown:
table.set_style(TableStyle.MARKDOWN)
table.field_names = data[0].keys()
table.header = args.header
table.align = "l"
for row in data:
table.add_row([row.get(col) for col in table.field_names])
if args.html:
print(table.get_html_string())
else:
print(table.get_string())
if __name__ == "__main__":
main()