从 Geopy 打印国家名称

Printing the country name from Geopy

我正在尝试使用 GeoPy 从 lat/long 对中打印特定国家/地区代码。它可以 return 地址、纬度、经度或整个 JSON 作为字典,但不能是单个组件。

有什么方法可以让我只访问国家/地区部分和 return 吗?这是我正在使用的代码,它输出来自地理定位器的原始响应:

from geopy.geocoders import Nominatim
geolocator = Nominatim()
Lat = input('Lat: ')
Long = input('Long: ')

location = geolocator.reverse([Lat, Long])
print(location.raw)

这是我收到的输出:

{'licence': 'Data © OpenStreetMap contributors, ODbL 1.0. http://www.openstreetmap.org/copyright', 'address': {'house_number': '1600', 'city': 'Washington', 'country_code': 'us', 'postcode': '20500', 'attraction': 'White House', 'neighbourhood': 'Monumental Core', 'country': 'United States of America', 'state': 'District of Columbia', 'pedestrian': 'Pennsylvania Avenue Northwest'}, 'display_name': 'White House, 1600, Pennsylvania Avenue Northwest, Monumental Core, Washington, District of Columbia, 20500, United States of America', 'lat': '38.8976989', 'boundingbox': ['38.8974898', '38.897911', '-77.0368539', '-77.0362521'], 'osm_id': '238241022', 'place_id': '2611224231', 'osm_type': 'way', 'lon': '-77.036553192281'}

简单地导航字典:

>>> print(location.raw['address']['country'])
United States of America
>>> print(location.raw['address']['country_code'])
us