1
|
#!/usr/bin/env python
|
2
|
# Inner-joins two map spreadsheets A->B and B->C to A->C
|
3
|
|
4
|
import csv
|
5
|
import sys
|
6
|
|
7
|
def merge_rows(*rows):
|
8
|
'''e.g. ['a','b'] + ['','y','z'] = ['a','b; y','z']'''
|
9
|
def get(row, i):
|
10
|
try: return row[i]
|
11
|
except IndexError: return ''
|
12
|
return ['; '.join(filter(lambda v: v != '', [get(row, i) for row in rows]))
|
13
|
for i in xrange(max(map(len, rows)))]
|
14
|
|
15
|
def main():
|
16
|
try: _prog_name, map_1_path = sys.argv
|
17
|
except ValueError:
|
18
|
raise SystemExit('Usage: '+sys.argv[0]+' <map_0 map_1 [| '+sys.argv[0]
|
19
|
+' map_2]... >joined_map')
|
20
|
|
21
|
# Get map 1
|
22
|
map_1 = {}
|
23
|
stream = open(map_1_path, 'rb')
|
24
|
reader = csv.reader(stream)
|
25
|
map_1_in, map_1_out = reader.next()[:2]
|
26
|
for row in reader:
|
27
|
if row[0] != '': map_1[row[0]] = row
|
28
|
stream.close()
|
29
|
|
30
|
# Join map 1 to map 0
|
31
|
reader = csv.reader(sys.stdin)
|
32
|
writer = csv.writer(sys.stdout)
|
33
|
cols = reader.next()
|
34
|
if not cols[1] == map_1_in: raise SystemExit('Map error: '
|
35
|
'Map 0 output column name doesn\'t match map 1 input column name')
|
36
|
cols[1] = map_1_out
|
37
|
writer.writerow(cols)
|
38
|
for row in reader:
|
39
|
if row[1] != '':
|
40
|
try:
|
41
|
new = map_1[row[1]]
|
42
|
row[1] = new[1] # mapping
|
43
|
row[2:] = merge_rows(row[2:], new[2:]) # comments
|
44
|
except KeyError:
|
45
|
row[2] = '** No join mapping for '+row[1]+' ** '+row[2]
|
46
|
row[1] = ''
|
47
|
writer.writerow(row)
|
48
|
|
49
|
main()
|