Project

General

Profile

« Previous | Next » 

Revision 4650

translate: Translate the column header instead of passing it through, in order to properly support CSVs without a header and to support renaming the header when the column's contents change to a different schema or vocabulary

View differences:

translate
1 1
#!/usr/bin/env python
2 2
# Translates a spreadsheet column using a dictionary.
3
# The column header is also translated. CSVs without a header are supported.
3 4
# Unrecognized names are left untouched, permitting successive runs on different
4 5
# dictionaries.
5 6
# Case- and punctuation-sensitive. (Use canon first for case-insensitivity.)
......
17 18
    dict_ = {}
18 19
    stream = open(dict_path, 'rb')
19 20
    reader = csv.reader(stream)
20
    reader.next() # skip header
21 21
    for row in reader: dict_[row[0]] = row[1]
22 22
    stream.close()
23 23
    
24 24
    # Translate input
25 25
    reader = csv.reader(sys.stdin)
26 26
    writer = csv.writer(sys.stdout)
27
    writer.writerow(reader.next()) # pass through header
28 27
    for row in reader:
29 28
        term = row[col_num]
30 29
        try: row[col_num] = dict_[term]

Also available in: Unified diff