-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathCountKeyValues.py
More file actions
executable file
·48 lines (41 loc) · 1.57 KB
/
CountKeyValues.py
File metadata and controls
executable file
·48 lines (41 loc) · 1.57 KB
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
#!/usr/bin/env python3
"""
# Purpose: Make a CSV file showing the number of instances of a key field
# Customize: Set KEY_FIELD, MIN_KEY_COUNT
# Python: Use python or python3 below as appropriate to your system; verify that you have version 3.9 or greater
# $ python -V or python3 -V
# Python 3.x.y
# Usage:
# 1: Generate a CSV file KeyValues.csv
# 2: From that CSV file, output a CSV file with headers "Key,Count" that shows the
# number of instances if each key value
# $ python3 CountKeyValues.py ./KeyValues.csv ./KeyValueCounts.csv
"""
import csv
import sys
QUOTE_CHAR = '"' # Adjust as needed
LINE_TERMINATOR = '\n' # On Windows, you probably want '\r\n'
KEY_FIELD = '' # Set to a column header in KeyValues.csv
MIN_KEY_COUNT = 0 # 0 - Show all counts; N - Show counts >= N
if (len(sys.argv) > 2) and (sys.argv[2] != '-'):
outputFile = open(sys.argv[2], 'w', encoding='utf-8', newline='')
else:
outputFile = sys.stdout
outputCSV = csv.DictWriter(outputFile, ['Key', 'Count'], lineterminator=LINE_TERMINATOR, quotechar=QUOTE_CHAR)
outputCSV.writeheader()
if (len(sys.argv) > 1) and (sys.argv[1] != '-'):
inputFile = open(sys.argv[1], 'r', encoding='utf-8')
else:
inputFile = sys.stdin
keyCounts = {}
for row in csv.DictReader(inputFile, quotechar=QUOTE_CHAR):
keyValue = row[KEY_FIELD]
keyCounts.setdefault(keyValue, 0)
keyCounts[keyValue] += 1
for key, count in sorted(keyCounts.items()):
if count >= MIN_KEY_COUNT:
outputCSV.writerow({'Key': key, 'Count': count})
if inputFile != sys.stdin:
inputFile.close()
if outputFile != sys.stdout:
outputFile.close()