Project

General

Profile

1
#!/usr/bin/env python
2
# Concatenates two spreadsheets with the same headers
3

    
4
import csv
5
import sys
6

    
7
def main():
8
    try: _prog_name, sheet_1_path = sys.argv
9
    except ValueError:
10
        raise SystemExit('Usage: '+sys.argv[0]+' <sheet_0 sheet_1 [| '
11
            +sys.argv[0]+' sheet_2]... >cat_sheet')
12
    
13
    # Get cols
14
    reader_0 = csv.reader(sys.stdin)
15
    sheet_0_cols = reader_0.next()
16
    stream_1 = open(sheet_1_path, 'rb')
17
    reader_1 = csv.reader(stream_1)
18
    sheet_1_cols = reader_1.next()
19
    if not sheet_0_cols == sheet_1_cols: raise SystemExit('Map error: '
20
        'Sheet 1 column names don\'t match sheet 0 column names')
21
    
22
    # Write combined sheet
23
    writer = csv.writer(sys.stdout)
24
    writer.writerow(sheet_0_cols)
25
    def write_sheet(reader):
26
        for row in reader: writer.writerow(row)
27
    write_sheet(reader_0)
28
    write_sheet(reader_1)
29
    
30
    stream_1.close()
31

    
32
main()
(1-1/19)