【问题标题】:How to fix `TypeError: argument of type 'ArcGIS' is not iterable` error on geopy?如何修复'TypeError:'ArcGIS'类型的参数在geopy上不可迭代'错误?
【发布时间】:2019-03-08 11:33:09
【问题描述】:

我想制作一个程序,您可以在其中输入您的地址(代码中的示例是荷兰地址),然后程序将输出该地址的经度和纬度。我还尝试使它对用户更友好,所以如果输入的地址不存在,程序会这样说。 代码是:

from geopy.geocoders import ArcGIS
nom = ArcGIS()
adres = input("enter your adress as folows:\n 32 Gunterstein, Amsterdam, 1081 CJ\n vul in: ")

n = nom.geocode(adres)
if adres in nom:
    print("longitude:",n.longitude)
    print("latitude:", n.latitude)
else:
    print("adress doesn't exist, please try again.")
print("end")

如果用户输入了一个有效的地址,代码就可以工作,但是当我通过输入废话来尝试时,我得到以下错误:

enter your adress as folows:
 32 Gunterstein, Amsterdam, 1081 CJ
 vul in: nonsense
Traceback (most recent call last):
  File "breede_en_lengte_graden.py", line 7, in <module>
    if adres in nom:
TypeError: argument of type 'ArcGIS' is not iterable

我收到该错误的代码有什么问题?

谢谢!

【问题讨论】:

  • 也许尝试使用tryexcept 而不是if else
  • 你理解if adres in nom应该怎么做?
  • @FlyingTeller 我认为 if 会检查地址是否有效,但是当我查看问题的答案时,情况并非如此

标签: python geopy


【解决方案1】:

这是使用try-except 块的一种方法:

try:
    print("longitude:", n.longitude)
    print("latitude:", n.latitude)
except AttributeError:
    print("adress doesn't exist, please try again.")
print("end")

您也可以使用 if-else 块来执行此操作,但您必须稍作不同:

if n is not None:
    print("longitude:", n.longitude)
    print("latitude:", n.latitude)
else:
    print("adress doesn't exist, please try again.")
print("end")

进行这种检查的原因是 nom.geocode(adres) 不会在无效地址上失败,而是简单地返回 None 并分配为 n 的值。

【讨论】:

  • 谢谢,帮了大忙
【解决方案2】:

我认为它不会在错误的地址上引发错误。

n = nom.geocode('sdf324uio')
n is None

是的

只需检查 n 是否为 None 即可查看是否传递了有效地址。

编辑:

有趣的是,nonsense 实际上作为地点存在并且它返回一个有效的位置(这是我第一次尝试不存在的地址):

n = nom.geocode('nonsense')
n

位置(废话,(-23.56400999999994,-46.66579999999993,0.0))

【讨论】:

  • 谢谢你的解释,我现在明白了
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-03-14
  • 1970-01-01
  • 2020-09-07
  • 2019-10-22
  • 2021-01-15
  • 2011-10-04
相关资源
最近更新 更多