-
Notifications
You must be signed in to change notification settings - Fork 0
/
underlords.sh
executable file
·41 lines (36 loc) · 1.06 KB
/
underlords.sh
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
#! /usr/bin/env python3
import csv
import sys
from collections import defaultdict
if len(sys.argv) != 2:
print('Need a heroes.csv file argument')
exit()
input_file = sys.argv[1]
Table = defaultdict(lambda: defaultdict(list))
Classes = set()
# Read into [Race][Class]=Heroes dictionary
# Keeping track of Classes to be used as header
with open(input_file, newline='') as csvfile:
parser = csv.DictReader(csvfile, delimiter=',')
for row in parser:
c = row['Class']
r = row['Race']
c2 = row['Class2']
r2 = row['Race2']
h = row['Hero']
Table[r][c].append(h)
if c2 != '':
Table[r][c2].append(h)
if r2 != '':
Table[r2][c].append(h)
Classes |= {c, c2}
# Write to csv, converting Heroes lists to strings seperated by newline
w = csv.DictWriter(sys.stdout, sorted(Classes) )
w.writeheader()
for key,val in (sorted(Table.items())):
strdict = {}
for k,v in val.items():
strdict[k] = '\n'.join(v)
row = {'': key}
row.update(strdict)
w.writerow(row)