Project

General

Profile

« Previous | Next » 

Revision 4655

Added cat_cols

View differences:

bin/cat_cols
1
#!/usr/bin/env python
2
# Concatenates spreadsheet columns in the order provided.
3
# The position and header of the combined column are that of the first column.
4

  
5
import csv
6
import sys
7

  
8
def main():
9
    try: _prog_name, col0_num, col1_num = sys.argv
10
    except ValueError: raise SystemExit('Usage: '+sys.argv[0]
11
        +' <in col0# col1# [| '+sys.argv[0]+' new_col0# new_col1#]... >out')
12
    col0_num, col1_num = map(int, [col0_num, col1_num])
13
    
14
    # Transform input
15
    reader = csv.reader(sys.stdin)
16
    writer = csv.writer(sys.stdout)
17
    is_header = True
18
    for row in reader:
19
        if is_header: is_header = False # don't concatenate headers
20
        else: row[col0_num] += row[col1_num]
21
        del row[col1_num]
22
        writer.writerow(row)
23

  
24
main()
0 25

  

Also available in: Unified diff