Project

General

Profile

1 51 aaronmk
#!/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 59 aaronmk
    reader = csv.reader(stream)
17 51 aaronmk
    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 59 aaronmk
    reader = csv.reader(sys.stdin)
24 51 aaronmk
    map_0_in, map_0_out = reader.next()[:2]
25
    assert map_0_out == map_1_in
26 59 aaronmk
    writer = csv.writer(sys.stdout)
27 51 aaronmk
    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()