-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.py
30 lines (23 loc) · 938 Bytes
/
parser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
from itertools import izip
def parse(input_file, output_file):
in_file = open(input_file, 'r')
lines = map(lambda o: o.strip().split(','), in_file.readlines())
groups = [{} for o in xrange(len(lines[0]))]
for line in lines:
filter(lambda o: o[0].update({o[1]: o[0].get(o[1], len(o[0]))}),
izip(groups, line))
with open(output_file, 'w') as out_file:
for line in lines:
out_file.write('%d' % groups[-1][line[-1]])
for position, value in enumerate(izip(groups, line[:-1]), start=1):
val = 1. / (len(value[0]) - 1) * value[0][value[1]]
out_file.write(' %d:%f' % (position, val))
out_file.write('\n')
if __name__ == '__main__':
import sys
input_file = sys.argv[1]
if len(sys.argv) == 2:
output_file = 'output.dat'
else:
output_file = sys.argv[2]
parse(input_file, output_file)