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 732 aaronmk
def merge_rows(*rows):
8
    '''e.g. ['a','b'] + ['','y','z'] = ['a','b; y','z']'''
9
    def get(row, i):
10
        try: return row[i]
11
        except IndexError: return ''
12
    return ['; '.join(filter(lambda v: v != '', [get(row, i) for row in rows]))
13
    for i in xrange(max(map(len, rows)))]
14
15 51 aaronmk
def main():
16 67 aaronmk
    try: _prog_name, map_1_path = sys.argv
17 51 aaronmk
    except ValueError:
18 180 aaronmk
        raise SystemExit('Usage: '+sys.argv[0]+' <map_0 map_1 [| '+sys.argv[0]
19
            +' map_2]... >joined_map')
20 51 aaronmk
21
    # Get map 1
22
    map_1 = {}
23
    stream = open(map_1_path, 'rb')
24 59 aaronmk
    reader = csv.reader(stream)
25 51 aaronmk
    map_1_in, map_1_out = reader.next()[:2]
26
    for row in reader:
27 732 aaronmk
        if row[0] != '': map_1[row[0]] = row
28 51 aaronmk
    stream.close()
29
30
    # Join map 1 to map 0
31 59 aaronmk
    reader = csv.reader(sys.stdin)
32 120 aaronmk
    writer = csv.writer(sys.stdout)
33
    cols = reader.next()
34 177 aaronmk
    if not cols[1] == map_1_in: raise SystemExit('Map error: '
35 62 aaronmk
        'Map 0 output column name doesn\'t match map 1 input column name')
36 120 aaronmk
    cols[1] = map_1_out
37
    writer.writerow(cols)
38 51 aaronmk
    for row in reader:
39 730 aaronmk
        if row[1] != '':
40 732 aaronmk
            try:
41
                new = map_1[row[1]]
42
                row[1] = new[1] # mapping
43
                row[2:] = merge_rows(row[2:], new[2:]) # comments
44 730 aaronmk
            except KeyError:
45
                row[2] = '** No join mapping for '+row[1]+' ** '+row[2]
46
                row[1] = ''
47 120 aaronmk
        writer.writerow(row)
48 51 aaronmk
49
main()