【问题标题】:Printing the country name from Geopy从 Geopy 打印国家名称
【发布时间】:2016-11-16 04:47:09
【问题描述】:

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

有没有办法只能访问国家/地区部分并将其返回?这是我正在使用的代码,用于输出地理定位器的原始响应:

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

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

这是我收到的输出:

{'licence': '数据 © OpenStreetMap 贡献者,ODbL 1.0。 http://www.openstreetmap.org/copyright', 'address': {'house_number': '1600', 'city': 'Washington', 'country_code': 'us', 'postcode': '20500', '景点': '白宫' , 'neighbourhood': 'Monumental Core', 'country': 'United States of America', 'state': 'District of Columbia', 'pedestrian': 'Pennsylvania Avenue Northwest'}, 'display_name': '白宫, 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'}

【问题讨论】:

    标签: python python-2.7 dictionary geocoding geopy


    【解决方案1】:

    只需浏览字典:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-27
      相关资源
      最近更新 更多