forked from senderle/tess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflip-index-headers.py
36 lines (31 loc) · 1.38 KB
/
flip-index-headers.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
# -------------------------------------------------------------------------------
# Imports
# -------------------------------------------------------------------------------
import csv
from collections import defaultdict
# -------------------------------------------------------------------------------
# Open the existing CSV to create an input dictionary
# -------------------------------------------------------------------------------
reader = csv.reader(open('index-headers.csv'))
input_dict = {} # initialize dictionary
for row in reader:
key = row[0] # the key is the first part of the row
if key in result:
# implement your duplicate row handling here
pass
result[key] = row[1:]
print(input_dict)
# -------------------------------------------------------------------------------
# Restructure the input dictionary for an output dictionary
# -------------------------------------------------------------------------------
output_dict = defaultdict(list)
for k, v in input_dict.items():
for e in v:
output_dict[e].append(k)
# -------------------------------------------------------------------------------
# Write the output dictionary to a csv
# -------------------------------------------------------------------------------
with open('index-pids.csv', 'wb') as f:
w = csv.DictWriter(f, output_dict.keys())
w.writeheader()
w.writerow(output_dict)