1 |
teichmann |
236 |
#!/usr/bin/env python |
2 |
|
|
# |
3 |
|
|
# (c) 2008 by Intevation GmbH |
4 |
|
|
# author: Sascha L. Teichmann ([email protected]) |
5 |
|
|
# |
6 |
|
|
import sys |
7 |
|
|
import codecs |
8 |
|
|
|
9 |
|
|
SEP = '\t' |
10 |
|
|
|
11 |
|
|
def main(): |
12 |
|
|
csvs = [] |
13 |
|
|
max_cols = -1; |
14 |
|
|
max_idx = -1; |
15 |
|
|
for idx, arg in enumerate(sys.argv[1:]): |
16 |
|
|
print >> sys.stderr, "file: %s" % arg |
17 |
|
|
f = None |
18 |
|
|
try: |
19 |
|
|
f = codecs.open(arg, "r", "latin1") |
20 |
|
|
csv = [] |
21 |
|
|
for line in f: |
22 |
|
|
line = line.replace('\r', '').replace('\n', '') |
23 |
|
|
if not line: continue |
24 |
|
|
line = line.split(SEP) |
25 |
|
|
l = len(line) |
26 |
|
|
line[0] = line[0].replace('#', '', 1) |
27 |
|
|
if l > max_cols: |
28 |
|
|
max_cols = l |
29 |
|
|
max_idx = idx |
30 |
|
|
csv.append(line) |
31 |
|
|
csvs.append(csv) |
32 |
|
|
finally: |
33 |
|
|
if f: |
34 |
|
|
try: f.close() |
35 |
|
|
except: pass |
36 |
|
|
|
37 |
|
|
print >> sys.stderr, "max_cols: %d" % max_cols |
38 |
|
|
print >> sys.stderr, "max_idx: %d" % max_idx |
39 |
|
|
|
40 |
|
|
maps = [] |
41 |
|
|
for i in csvs: |
42 |
|
|
maps.append([-1] * max_cols) |
43 |
|
|
|
44 |
|
|
master = csvs[max_idx] |
45 |
|
|
|
46 |
|
|
for j, m in enumerate(master[0]): |
47 |
|
|
for i in range(len(csvs)): |
48 |
|
|
try: |
49 |
|
|
idx = csvs[i][0].index(m) |
50 |
|
|
maps[i][j] = idx |
51 |
|
|
except ValueError: |
52 |
|
|
pass |
53 |
|
|
|
54 |
|
|
Writer = codecs.getwriter("latin-1") |
55 |
|
|
f = Writer(sys.stdout) |
56 |
|
|
f.write("#%s\r\n" % SEP.join(master[0])) |
57 |
|
|
|
58 |
|
|
for row, csv in enumerate(csvs): |
59 |
|
|
map = maps[row] |
60 |
|
|
for c in csv[1:]: |
61 |
|
|
line = [] |
62 |
|
|
for j in xrange(max_cols): |
63 |
|
|
idx = map[j] |
64 |
|
|
if idx >= 0: |
65 |
|
|
line.append(c[idx]) |
66 |
|
|
else: |
67 |
|
|
line.append(SEP) |
68 |
|
|
f.write("%s\r\n" % SEP.join(line)) |
69 |
|
|
|
70 |
|
|
if __name__ == "__main__": |
71 |
|
|
main() |