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
|
#!/usr/bin/env python3
import csv
import simplejson as json
import operator
def dump_json():
name_to_tel = {}
name_to_iso = {}
iso_to_name = {}
iso_to_tel = {}
tel_to_name = {}
tel_to_iso = {}
with open('components/iso_3166_2_countries.csv', encoding="utf-8") as f:
reader = csv.reader(f)
for row in reader:
name = row[1]
iso = row[10] # two letter country code
tel = row[9]
if not tel or not iso:
continue
name_to_tel[name] = tel
name_to_iso[name] = iso
iso_to_name[iso] = name
iso_to_tel[iso] = tel
tel_to_name[tel] = name
tel_to_iso[tel] = iso
with open('components/country_data.js', 'w') as f:
f.write("var name_to_tel = ")
f.write(json.dumps(name_to_tel))
f.write(";\n")
f.write("var name_to_iso = ")
f.write(json.dumps(name_to_iso))
f.write(";\n")
f.write("var iso_to_name = ")
f.write(json.dumps(iso_to_name))
f.write(";\n")
f.write("var iso_to_tel = ")
f.write(json.dumps(iso_to_tel))
f.write(";\n")
f.write("var tel_to_name = ")
f.write(json.dumps(tel_to_name))
f.write(";\n")
f.write("var tel_to_iso = ")
f.write(json.dumps(tel_to_iso))
if __name__ == "__main__":
dump_json()
|