/[formed]/trunk/tools/anonym/joincsv.py
ViewVC logotype

Contents of /trunk/tools/anonym/joincsv.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 238 - (show annotations)
Mon Feb 25 17:08:20 2008 UTC (17 years ago) by teichmann
File MIME type: text/x-python
File size: 1862 byte(s)
Merged

1 #!/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('')
68 all = SEP.join(line)
69 f.write("%s\r\n" % all)
70
71 if __name__ == "__main__":
72 main()

Properties

Name Value
svn:executable *

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26