Project

General

Profile

1
#!/usr/bin/env python
2
# Collapses multimap entries in a spreadsheet dictionary.
3
# A multimap entry is any input with multiple outputs.
4
# Collapsed entries will have the form ?alt0,alt1,...
5
# CSVs without a header are supported.
6

    
7
from collections import OrderedDict
8
import csv
9
import sys
10

    
11
def main():
12
    # Read dict
13
    dict_ = OrderedDict()
14
    reader = csv.reader(sys.stdin)
15
    for row in reader: dict_.setdefault(row[0], []).append(row[1])
16
    
17
    # Write modified dict
18
    writer = csv.writer(sys.stdout)
19
    for in_, out in dict_.iteritems():
20
        out_prefix = ''
21
        if len(out) > 1: out_prefix = '?'
22
        writer.writerow([in_, out_prefix+(','.join(out))])
23

    
24
main()
(11-11/87)