Project

General

Profile

1
#!/usr/bin/env python
2
# Translates a spreadsheet column using a dictionary.
3
# The column header is also translated. CSVs without a header are supported.
4
# Unrecognized names are left untouched, permitting successive runs on different
5
# dictionaries.
6
# Case- and punctuation-sensitive. (Use canon first for case-insensitivity.)
7

    
8
import csv
9
import sys
10

    
11
def main():
12
    try: _prog_name, col_num, dict_path = sys.argv
13
    except ValueError: raise SystemExit('Usage: '+sys.argv[0]
14
        +' <in col# dict [| '+sys.argv[0]+' col# dict_2]... >out')
15
    col_num = int(col_num)
16
    
17
    # Get dict
18
    dict_ = {}
19
    stream = open(dict_path, 'rb')
20
    reader = csv.reader(stream)
21
    for row in reader: dict_[row[0]] = row[1]
22
    stream.close()
23
    
24
    # Translate input
25
    reader = csv.reader(sys.stdin)
26
    writer = csv.writer(sys.stdout)
27
    for row in reader:
28
        term = row[col_num]
29
        try: row[col_num] = dict_[term]
30
        except KeyError: pass
31
        writer.writerow(row)
32

    
33
main()
(56-56/60)