-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatcher.py
64 lines (55 loc) · 1.3 KB
/
matcher.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import re
import sys
import os
import time
import json
def inputs_to_jsons(path):
city_cc = re.compile(r"(\'.*?\')\|(\'[A-Z]{2}\')")
inp = open(path, 'r')
in_data = []
dic = {}
i = 0
l = True
while l:
try:
line = inp.readline()
if line == '':
l = False
if line in dic:
dic[line] += 1
else:
dic[line] = 1
m = city_cc.match(line)
print(f'{i}:{line} => match {m}')
if m is not None:
in_data.append((m[0], m[1], m[2]))
except:
print(f'Exception at line {i}')
i += 1
print(in_data[:20])
f = open('curated_input.txt', 'w')
json.dump(in_data, f)
f.close()
f = open('merged.txt', 'w')
json.dump(dic, f)
f.close()
inp.close()
def unique_city_name(l):
dic = {}
for e in l:
print(e)
if e[1] in dic:
if e[2] in dic[e[1]]:
dic[e[1]][e[2]] += 1
else:
dic[e[1]][e[2]] = 1
else:
dic[e[1]] = {e[2] : 1}
f = open('unique_city.txt', 'w')
json.dump(dic, f)
f.close()
if __name__ == '__main__':
f = open('curated_input.txt', 'r')
l = json.load(f)
f.close()
unique_city_name(l)