Project

General

Profile

« Previous | Next » 

Revision 738

union: Merge rows (and column labels) using maps.merge_mappings()

View differences:

union
2 2
# Combines two map spreadsheets A0->B and A1->C to A->B, with A0 overwriting A1
3 3

  
4 4
import csv
5
import os.path
5 6
import sys
6 7

  
8
sys.path.append(os.path.dirname(__file__)+"/../lib")
9

  
10
import maps
11

  
7 12
def main():
8 13
    try: _prog_name, map_1_path = sys.argv
9 14
    except ValueError:
......
13 18
    map_ = {}
14 19
    def add_map(reader):
15 20
        for row in reader:
16
            if row[1] != '': map_[row[0]] = row[1:]
21
            if row[0] != '':
22
                try: new = map_[row[0]]
23
                except KeyError: new = row
24
                else: new = maps.merge_mappings(new, row)
25
                map_[row[0]] = new
17 26
    
18 27
    # Get map 1
19 28
    stream = open(map_1_path, 'rb')
......
25 34
    # Add map 0 to map 1, overwriting existing entries
26 35
    reader = csv.reader(sys.stdin)
27 36
    map_0_cols = reader.next()
28
    if not map_0_cols[0] == map_1_cols[0]: raise SystemExit('Map error: '
37
    if map_0_cols[0] != map_1_cols[0]: raise SystemExit('Map error: '
29 38
        'Map 1 column 0 name doesn\'t match map 0 column 0 name')
30 39
    add_map(reader)
31 40
    
32 41
    # Write combined map
33 42
    writer = csv.writer(sys.stdout)
34
    writer.writerow(map_0_cols)
35
    for in_, rest in map_.iteritems(): writer.writerow([in_]+rest)
43
    writer.writerow(maps.merge_mappings(map_1_cols, map_0_cols))
44
    for row in map_.itervalues(): writer.writerow(row)
36 45

  
37 46
main()

Also available in: Unified diff