Geopy: retrieving country names in English

隐身守侯 提交于 2020-02-20 09:52:21

问题


I am trying to reverse geocode coordinates and retrieve corresponding country codes using geopy. However, Geopy does not seem to provide a method for fetching country codes. So, I am trying to retrieve country names first, and then convert them to codes. Unfortunately, my code gives me country names in non-English languages.

How can I fetch country names in English ?

My code:

geolocator = Nominatim()
....
with open('coordinates.txt' , 'r') as readfile:
for line in readfile:
    fields = line.split("\t")
    address, (latitude, longitude) = geolocator.reverse(fields[1]+","+fields[2])
    if address:
        address = address.split(",")
        print "%s" % (address[-1])

The output I am getting:

Ελλάδα
Україна
Türkiye
Shqipëria
Tanzania
ኢትዮጵያ Ethiopia
Bosna i Hercegovina
Türkiye
Shqipëria
România
السودان - Sudan

回答1:


Pass in the language parameter to the reverse() request, e.g.:

geolocator.reverse(','.join(fields[1:3]), language='en')


来源:https://stackoverflow.com/questions/29360910/geopy-retrieving-country-names-in-english

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!