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
|
try: _prog_name, map_1_path = sys.argv
|
9
|
except ValueError:
|
10
|
raise SystemExit('Usage: '+sys.argv[0]+' map_1 <map_0 >joined_map')
|
11
|
|
12
|
# Get map 1
|
13
|
map_1 = {}
|
14
|
stream = open(map_1_path, 'rb')
|
15
|
reader = csv.reader(stream)
|
16
|
map_1_in, map_1_out = reader.next()[:2]
|
17
|
for row in reader:
|
18
|
if row[1] != '': map_1[row[0]] = row[1]
|
19
|
stream.close()
|
20
|
|
21
|
# Join map 1 to map 0
|
22
|
reader = csv.reader(sys.stdin)
|
23
|
map_0_in, map_0_out = reader.next()[:2]
|
24
|
if not map_0_out == map_1_in: raise SystemExit( 'Map error: '
|
25
|
'Map 0 output column name doesn\'t match map 1 input column name')
|
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()
|