Revision 732
Added by Aaron Marcuse-Kubitza almost 13 years ago
bin/join | ||
---|---|---|
4 | 4 |
import csv |
5 | 5 |
import sys |
6 | 6 |
|
7 |
def merge_rows(*rows): |
|
8 |
'''e.g. ['a','b'] + ['','y','z'] = ['a','b; y','z']''' |
|
9 |
def get(row, i): |
|
10 |
try: return row[i] |
|
11 |
except IndexError: return '' |
|
12 |
return ['; '.join(filter(lambda v: v != '', [get(row, i) for row in rows])) |
|
13 |
for i in xrange(max(map(len, rows)))] |
|
14 |
|
|
7 | 15 |
def main(): |
8 | 16 |
try: _prog_name, map_1_path = sys.argv |
9 | 17 |
except ValueError: |
... | ... | |
16 | 24 |
reader = csv.reader(stream) |
17 | 25 |
map_1_in, map_1_out = reader.next()[:2] |
18 | 26 |
for row in reader: |
19 |
if row[0] != '': map_1[row[0]] = row[1]
|
|
27 |
if row[0] != '': map_1[row[0]] = row |
|
20 | 28 |
stream.close() |
21 | 29 |
|
22 | 30 |
# Join map 1 to map 0 |
... | ... | |
29 | 37 |
writer.writerow(cols) |
30 | 38 |
for row in reader: |
31 | 39 |
if row[1] != '': |
32 |
try: row[1] = map_1[row[1]] |
|
40 |
try: |
|
41 |
new = map_1[row[1]] |
|
42 |
row[1] = new[1] # mapping |
|
43 |
row[2:] = merge_rows(row[2:], new[2:]) # comments |
|
33 | 44 |
except KeyError: |
34 | 45 |
row[2] = '** No join mapping for '+row[1]+' ** '+row[2] |
35 | 46 |
row[1] = '' |
Also available in: Unified diff
join: Merge comments of input map and join map