equal
deleted
inserted
replaced
|
1 """Module to fetch data. |
|
2 """ |
|
3 |
|
4 __authors__ = [ |
|
5 '"Madhusudan.C.S" <madhusudancs@gmail.com>', |
|
6 ] |
|
7 |
|
8 |
|
9 import json |
|
10 |
|
11 |
|
12 def fetch_state_code(file_name): |
|
13 """Fetch State Codes |
|
14 """ |
|
15 |
|
16 fh = file(file_name) |
|
17 states = {} |
|
18 for line in fh: |
|
19 name, code = line.split('\t') |
|
20 name = name.strip() |
|
21 code = code.strip() |
|
22 states[code] = name |
|
23 print json.dumps(states, indent=4) |
|
24 |
|
25 def fetch_district_code(file_name, write_file): |
|
26 """Fetch District Codes |
|
27 """ |
|
28 |
|
29 fh = file(file_name) |
|
30 districts = {} |
|
31 for line in fh: |
|
32 if line == '\n' or line[:4] == 'See ' or line[:4] == 'Code': |
|
33 continue |
|
34 if line[:6] == '[edit]': |
|
35 state = line[6:].strip().split()[-1].strip('()') |
|
36 continue |
|
37 district_line = line.split('\t') |
|
38 code = '%s%s' % (state, district_line[0].strip()) |
|
39 name = district_line[1].strip() |
|
40 districts[code] = name |
|
41 if len(code) > 4: |
|
42 print code, name |
|
43 fh.close() |
|
44 district_data = json.dumps(districts, indent=6) |
|
45 fh = file(write_file, "w") |
|
46 fh.write(district_data) |
|
47 fh.close() |
|
48 |
|
49 if __name__ == '__main__': |
|
50 import sys |
|
51 if sys.argv[1] == 'fetch_state_code': |
|
52 fetch_state_code(sys.argv[2]) |
|
53 if sys.argv[1] == 'fetch_district_code': |
|
54 fetch_district_code(sys.argv[2], sys.argv[3]) |