Project

General

Profile

1
#!/usr/bin/env python
2
# Selects columns from a spreadsheet
3
# CSVs without a header are supported.
4

    
5
import csv
6
import os.path
7
import sys
8

    
9
sys.path.append(os.path.dirname(__file__)+"/../lib")
10

    
11
import format
12
import util
13

    
14
def main():
15
    col_nums = sys.argv[1:]
16
    if col_nums == []:
17
        raise SystemExit('Usage: '+sys.argv[0]+' col_num... <map >columns_map')
18
    col_nums = map(format.str2int_passthru, col_nums) # 0-based
19
    
20
    # Process map
21
    reader = csv.reader(sys.stdin)
22
    writer = csv.writer(sys.stdout)
23
    def subset(row): return util.list_subset(row, col_nums)
24
    for row in reader: writer.writerow(subset(row))
25

    
26
main()
(9-9/63)