-
Notifications
You must be signed in to change notification settings - Fork 1
/
table-summarize.py
39 lines (29 loc) · 1.18 KB
/
table-summarize.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
#! /usr/bin/env python
from __future__ import print_function
import csv
import sys
from collections import Counter, OrderedDict
def main(table):
with open(
table, "r", newline="", encoding="utf-8"
) as table_f: # Improved file opening
rdr = csv.DictReader(table_f, delimiter="\t", dialect="excel")
# Check if fieldnames exist before proceeding to avoid potential errors
if not rdr.fieldnames or len(rdr.fieldnames) <= 1:
print("No data columns found in the table.")
return
summary = OrderedDict()
for row in rdr: # Iterate directly without creating a list in memory
for name in rdr.fieldnames[1:]:
summary.setdefault(name, Counter()).update(
[row[name]]
) # More efficient counting
total = rdr.line_num - 1 # get the number of rows
print("Summary:")
for name, results in summary.items():
print(f"{name}:") # f-string
for result, num in results.items():
if result:
print(f"\t - {result}: {num} of {total}") # f-string
if __name__ == "__main__":
main(sys.argv[1])