python geopy 获取城市和国家

4
我正在尝试使用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'])

这是输出内容:
{'suburb': 'Tiergarten', 'country_code': 'de', 'country': 'Deutschland', 'postcode': '10117', 'attraction': 'Potsdamer Platz', 'road': 'Potsdamer Platz', 'city': 'Berlin', 'city_district': 'Mitte', 'state': 'Berlin'}

我的问题是,geopy返回的城市和国家名称是以它们的国家语言呈现的,例如:

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个回答

7
reverse()函数中添加语言应该可以解决您的问题:
geolocator.reverse(location, language='en')

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接