Project

General

Profile

1
#!/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 format
11
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
    col_nums = map(format.str2int_passthru, col_nums) # 0-based
18
    
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()
(6-6/58)