1 |
935
|
aaronmk
|
#!/usr/bin/env python
|
2 |
|
|
# Selects columns from a spreadsheet
|
3 |
4662
|
aaronmk
|
# CSVs without a header are supported.
|
4 |
935
|
aaronmk
|
|
5 |
|
|
import csv
|
6 |
|
|
import os.path
|
7 |
|
|
import sys
|
8 |
|
|
|
9 |
|
|
sys.path.append(os.path.dirname(__file__)+"/../lib")
|
10 |
|
|
|
11 |
3817
|
aaronmk
|
import format
|
12 |
935
|
aaronmk
|
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 |
3817
|
aaronmk
|
col_nums = map(format.str2int_passthru, col_nums) # 0-based
|
19 |
935
|
aaronmk
|
|
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()
|