Project

General

Profile

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
    csv_config = dict(delimiter=',', quotechar='"')
14
    
15
    # Get map 1
16
    map_1 = {}
17
    stream = open(map_1_path, 'rb')
18
    reader = csv.reader(stream, **csv_config)
19
    map_1_in, map_1_out = reader.next()[:2]
20
    for row in reader:
21
        if row[1] != '': map_1[row[0]] = row[1]
22
    stream.close()
23
    
24
    # Join map 1 to map 0
25
    reader = csv.reader(sys.stdin, **csv_config)
26
    map_0_in, map_0_out = reader.next()[:2]
27
    assert map_0_out == map_1_in
28
    writer = csv.writer(sys.stdout, **csv_config)
29
    writer.writerow([map_0_in, map_1_out])
30
    for row in reader:
31
        if row[1] in map_1: writer.writerow([row[0], map_1[row[1]]])
32

    
33
main()
(5-5/11)