comparison table-summarize.py @ 0:402b58f45844 draft default tip

planemo upload commit 9cc4dc1db55299bf92ec6bd359161ece4592bd16-dirty
author jpayne
date Mon, 08 Dec 2025 15:03:06 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:402b58f45844
1 #! /usr/bin/env python
2
3 from __future__ import print_function
4
5 import csv
6 import sys
7 from collections import Counter, OrderedDict
8
9
10 def main(table):
11 with open(
12 table, "r", newline="", encoding="utf-8"
13 ) as table_f: # Improved file opening
14 rdr = csv.DictReader(table_f, delimiter="\t", dialect="excel")
15
16 # Check if fieldnames exist before proceeding to avoid potential errors
17 if not rdr.fieldnames or len(rdr.fieldnames) <= 1:
18 print("No data columns found in the table.")
19 return
20
21 summary = OrderedDict()
22 for row in rdr: # Iterate directly without creating a list in memory
23 for name in rdr.fieldnames[1:]:
24 summary.setdefault(name, Counter()).update(
25 [row[name]]
26 ) # More efficient counting
27
28 total = rdr.line_num - 1 # get the number of rows
29
30 print("Summary:")
31 for name, results in summary.items():
32 print(f"{name}:") # f-string
33 for result, num in results.items():
34 if result:
35 print(f"\t - {result}: {num} of {total}") # f-string
36
37
38 if __name__ == "__main__":
39 main(sys.argv[1])