Geopy:以英语检索国家/地区名称 [英] Geopy: retrieving country names in English

查看:134
本文介绍了Geopy:以英语检索国家/地区名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试反转地理编码坐标,并使用geopy检索相应的国家/地区代码.但是,Geopy似乎没有提供一种获取国家/地区代码的方法.因此,我尝试首先检索国家/地区名称,然后将其转换为代码.不幸的是,我的代码给了我非英语语言的国家名称.

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.

如何获取英文国家名称?

我的代码:

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])

我得到的输出:

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

推荐答案

将language参数传递给reverse()请求,例如:

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

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

这篇关于Geopy:以英语检索国家/地区名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆