Replies: 1 comment
-
|
You can find the country for a city name by searching the full cities dataset and retrieving its associated country info. The database includes each city’s name, state, and country fields, but city names aren’t unique globally—so searching by city name alone may return multiple results from different countries. There’s no built-in “city name only” search, but you can filter the exported cities data (JSON, SQL, MongoDB, etc.) for your city name and read the country_code or country_id field from the result. For example, in SQL: SELECT name, country_code, state_code FROM cities WHERE name = 'Springfield';Or in Python with the JSON export: import json
with open('cities.json') as f:
cities = json.load(f)
matches = [c for c in cities if c['name'].lower() == 'springfield']
for city in matches:
print(city['name'], city['country_code'], city['state_code'])If there are multiple matches, you’ll need to pick the right one based on state or country. Official NPM, PyPI packages, and the REST API exist, but you’ll still need to filter results by city name to get the country info you want. To reply, just mention @dosu. How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Right now I'm in a situation where I need to get the country that a city is in, and all I have is the city name. I see there is a city search, but it requires the country and state.
Is there a way to do what I need?
Beta Was this translation helpful? Give feedback.
All reactions