Project

General

Profile

1 935 aaronmk
#!/usr/bin/env python
2
# Selects columns from a spreadsheet
3
4
import csv
5
import os.path
6
import sys
7
8
sys.path.append(os.path.dirname(__file__)+"/../lib")
9
10 3817 aaronmk
import format
11 935 aaronmk
import util
12
13
def main():
14
    col_nums = sys.argv[1:]
15
    if col_nums == []:
16
        raise SystemExit('Usage: '+sys.argv[0]+' col_num... <map >columns_map')
17 3817 aaronmk
    col_nums = map(format.str2int_passthru, col_nums) # 0-based
18 935 aaronmk
19
    # Process map
20
    reader = csv.reader(sys.stdin)
21
    writer = csv.writer(sys.stdout)
22
    def subset(row): return util.list_subset(row, col_nums)
23
    writer.writerow(subset(reader.next()))
24
    for row in reader: writer.writerow(subset(row))
25
26
main()