forked from taers232c/GAM-Scripts3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CombineKeyValues.py
executable file
·53 lines (44 loc) · 1.71 KB
/
CombineKeyValues.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
#!/usr/bin/env python3
"""
# Purpose: Make a CSV file that merges all values for a given key.
# Note: This script can use Basic or Advanced GAM:
# https://github.com/GAM-team/GAM
# https://github.com/taers232c/GAMADV-XTD3
# Customize: Set KEY_FIELD and VALUE_FIELD
# Python: Use python or python3 below as appropriate to your system; verify that you have version 3
# $ python -V or python3 -V
# Python 3.x.y
# Usage:
# 1: Generate CSV file KeySingleValue.csv with columns KEY_FIELD and VALUE_FIELD, one key and value per row
# 2: Output an updated CSV file with columns KEY_FIELD and VALUE_FIELD containing a row per key
# with its merged (space separated) values
# $ python3 CombineKeyValues.py ./KeyValue.csv ./KeyMergedValues.csv
"""
import csv
import sys
# Name of key field
KEY_FIELD = 'key'
# Name of value field
VALUE_FIELD = 'value'
QUOTE_CHAR = '"' # Adjust as needed
LINE_TERMINATOR = '\n' # On Windows, you probably want '\r\n'
keyValues = {}
if (len(sys.argv) > 1) and (sys.argv[1] != '-'):
inputFile = open(sys.argv[1], 'r', encoding='utf-8')
else:
inputFile = sys.stdin
for row in csv.DictReader(inputFile, quotechar=QUOTE_CHAR):
key = row[KEY_FIELD]
keyValues.setdefault(key, set())
keyValues[key].add(row[VALUE_FIELD])
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_FIELD, VALUE_FIELD], lineterminator=LINE_TERMINATOR, quotechar=QUOTE_CHAR)
outputCSV.writeheader()
for key, values in sorted(iter(keyValues.items())):
outputCSV.writerow({KEY_FIELD: key, VALUE_FIELD: ' '.join(values)})
inputFile.close()
if outputFile != sys.stdout:
outputFile.close()