Reputation: 75
I want to get all localities/regions of a city with python. Currently I have all countries and their provinces of them.
But now I don't know how to get all regions of every city. I tested many libraries such as pycountry, geograpy, etc... but I haven't found it yet.
import pycountry
countries = {}
for country in all_countries:
subdivisions = list(pycountry.subdivisions.get(country_code = country.alpha_2))
provinces = [sub for sub in subdivisions if sub.type == "Province"]
all_provinces = {province.name:{"code":province.code,"cities":{}} for province in provinces}
countries.update({country.name:{"alpha_2":country.alpha_2,
"alpha_3":country.alpha_3,
"numeric":country.numeric,
#"official_name":country.official_name
"provinces":all_provinces
}})
Upvotes: 2
Views: 2961
Reputation: 13166
You are looking into the wrong place. pycountry is mean to give you simple country related information.
If you need to dig into the city, use another database that deal with atomise regional details.
Maxmind currently open up a free city database that you should look into.
Upvotes: 1