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 main():
|
8
|
prog_name = sys.argv[0]
|
9
|
try: prog_name, map_1_path = sys.argv
|
10
|
except ValueError:
|
11
|
raise Exception('Usage: '+prog_name+' map_1 <map_0 >joined_map')
|
12
|
|
13
|
# Get map 1
|
14
|
map_1 = {}
|
15
|
stream = open(map_1_path, 'rb')
|
16
|
reader = csv.reader(stream)
|
17
|
map_1_in, map_1_out = reader.next()[:2]
|
18
|
for row in reader:
|
19
|
if row[1] != '': map_1[row[0]] = row[1]
|
20
|
stream.close()
|
21
|
|
22
|
# Join map 1 to map 0
|
23
|
reader = csv.reader(sys.stdin)
|
24
|
map_0_in, map_0_out = reader.next()[:2]
|
25
|
assert map_0_out == map_1_in
|
26
|
writer = csv.writer(sys.stdout)
|
27
|
writer.writerow([map_0_in, map_1_out])
|
28
|
for row in reader:
|
29
|
if row[1] in map_1: writer.writerow([row[0], map_1[row[1]]])
|
30
|
|
31
|
main()
|