Project

General

Profile

1 929 aaronmk
#!/usr/bin/env python
2
# Subtracts map spreadsheet A1->C from A0->B to A->B
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]... >subtraction_map')
12
13
    # Get map 1
14
    map_1 = {}
15
    stream = open(map_1_path, 'rb')
16
    reader = csv.reader(stream)
17
    map_1_cols = reader.next()
18
    for row in reader:
19
        if row[0] != '': map_1[row[0]] = row
20
    stream.close()
21
22
    # Open map 0
23
    reader = csv.reader(sys.stdin)
24
    map_0_cols = reader.next()
25
    if map_0_cols[0] != map_1_cols[0]: raise SystemExit('Map error: '
26
        'Map 1 column 0 name doesn\'t match map 0 column 0 name')
27
28
    # Subtract map 1 from map 0
29
    writer = csv.writer(sys.stdout)
30
    writer.writerow(map_0_cols)
31
    for row in reader:
32
        if row[0] not in map_1: writer.writerow(row)
33
34
main()