-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFilterCountriesStatesCities.py
More file actions
46 lines (33 loc) · 1.07 KB
/
FilterCountriesStatesCities.py
File metadata and controls
46 lines (33 loc) · 1.07 KB
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
import json
import requests as rq
res = rq.get('https://raw.githubusercontent.com/dr5hn/countries-states-cities-database/refs/heads/master/json/countries%2Bstates%2Bcities.json')
if not res.ok:
print('Country data is not available right now')
exit()
data = json.loads(res.text)
'''
with open('countries+states+cities.json', mode="r", encoding="utf-8") as openfile:
data = json.load(openfile)
'''
new_data = []
for country in data:
countryData = {
"name": country["name"],
"states": []
}
for state in country["states"]:
stateData = {
"name": state["name"],
"cities": []
}
for city in state["cities"]:
stateData["cities"].append({
"name": city["name"],
})
countryData["states"].append(stateData)
new_data.append(countryData)
# Serializing json
json_object = json.dumps(new_data, indent=4, ensure_ascii=False)
# Writing to sample.json
with open("countries_states_cities.json", mode="w", encoding="utf-8") as outfile:
outfile.write(json_object)