python geopy得到城市和国家

时间:2017-10-18 11:52:03

标签: python geopy

我正在尝试使用geopy获取城市和国家/地区名称。

通常情况下,您可以使用地理位置来获取城市和国家。

from geopy.geocoders import Nominatim

geolocator = Nominatim(timeout=3)
geolocator.reverse('52.5094982,13.3765983')
loc = location.raw
loc_dict = location.raw
print(loc_dict['address'])
and this is the output:

{'suburb': 'Tiergarten', 'country_code': 'de', 'country': 'Deutschland', 'postcode': '10117', 'attraction': 'Potsdamer Platz', 'road': 'Potsdamer Platz', 'city': 'Berlin', 'city_district': 'Mitte', 'state': 'Berlin'}

我的问题是,地理位置会以其国家/地区语言返回城市和国家/地区名称,例如:

city: Berlin, country: Deutschland
city: København, country: Danmark
city: București, country: România
我希望他们这样:

city: Berlin, country: Germany
city: Copenhagen, country: Denmark
city: Bucharest, country: Romania

是否有可能按照我的意愿将它们用英语?或以某种方式转换它们?我正在使用世界各地的所有国家和城市。

1 个答案:

答案 0 :(得分:2)

reverse()函数中添加语言可以解决您的问题:

geolocator.reverse(location, language='en')
相关问题