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
import util
11
12
def main():
13
    col_nums = sys.argv[1:]
14
    if col_nums == []:
15
        raise SystemExit('Usage: '+sys.argv[0]+' col_num... <map >columns_map')
16 1496 aaronmk
    if col_nums[0] == '*': col_nums = None # list_subset() value for all columns
17
    else: col_nums = map(int, 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()