Project

General

Profile

1
#!/usr/bin/env python
2
# Combines two map spreadsheets A0->B and A1->C to A->B, with A0 overwriting A1
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_0 map_1 [| '+sys.argv[0]
11
            +' map_2]... >union_map')
12
    
13
    map_ = {}
14
    def add_map(reader):
15
        for row in reader:
16
            if row[1] != '': map_[row[0]] = row[1:]
17
    
18
    # Get map 1
19
    stream = open(map_1_path, 'rb')
20
    reader = csv.reader(stream)
21
    map_1_cols = reader.next()
22
    add_map(reader)
23
    stream.close()
24
    
25
    # Add map 0 to map 1, overwriting existing entries
26
    reader = csv.reader(sys.stdin)
27
    map_0_cols = reader.next()
28
    if not map_0_cols[0] == map_1_cols[0]: raise SystemExit('Map error: '
29
        'Map 1 column 0 name doesn\'t match map 0 column 0 name')
30
    add_map(reader)
31
    
32
    # Write combined map
33
    writer = csv.writer(sys.stdout)
34
    writer.writerow(map_0_cols)
35
    for in_, rest in map_.iteritems(): writer.writerow([in_]+rest)
36

    
37
main()
(16-16/17)