【问题标题】:How to Loop Over a list of addresses for Longitude and Latitude (Python)如何遍历经度和纬度的地址列表(Python)
【发布时间】:2019-09-14 05:08:57
【问题描述】:

我正在尝试遍历我拥有的地址列表(街道名称、城市、州、邮政编码)以获得经度和纬度

    for address in addresses:
          g = geolocator.geocode(address)
          print(g.address)
          print((g.latitude, g.longitude))
          LatLong.append((g.latitude, g.longitude))

当我运行此代码时,我收到列表中第一个地址的 long 和 lat,然后得到:

AttributeError: 'NoneType' object has no attribute 'address'

非常感谢任何帮助。

【问题讨论】:

    标签: python geocoding geocoder


    【解决方案1】:

    当没有找到结果时,geopy 中的至少一些地理定位器返回None,如in the documentation 所示。在假设返回结果之前,您必须检查返回值。

    【讨论】:

    • 谢谢。立即阅读文档
    • 当我手动输入地理编码器在循环期间找不到的地址时。有希望将其转换回循环吗?底层地址的格式都相同
    • @ynnad 你确定你使用的是同一个地理编码器吗?如果您再次通过循环运行它,它仍然会失败吗?两个字符串之间是否可能存在一些小的(甚至是不可见的)差异,例如不可打印的字符?
    【解决方案2】:

    在尝试使用之前检查g 是否为None

    for address in addresses:
      g = geolocator.geocode(address)
    
      if g is None:
        print ('{} could not be geocoded'.format(address))
      else:
        print(g.address)
        print((g.latitude, g.longitude))
        LatLong.append((g.latitude, g.longitude))
    

    【讨论】:

    • 谢谢。这适用于大多数地址。然而,其余的确实有 g = none。 (这里是新编码器,我不理解为什么的逻辑。其中 g = none,地址存在,那么为什么 g = none?)
    • @ynnad, g 没有,因为地理编码器无法找到地址。见other answer
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-30
    • 2011-04-18
    • 2016-03-25
    • 1970-01-01
    • 1970-01-01
    • 2011-04-04
    相关资源
    最近更新 更多