【问题标题】:Error using Google distance matrix API with Python在 Python 中使用 Google 距离矩阵 API 时出错
【发布时间】:2016-06-08 13:12:48
【问题描述】:

这是我的代码:

from googlemaps import Client as GoogleMaps
mapServices = GoogleMaps('')
start = 'texarkana'
end   = 'atlanta'
direction = mapServices.directions(start, end)
for step in direction['Direction']['Routes'][0]['Steps']:
    print(step['descriptionHtml'])

我收到以下错误:

File "C:\Python27\directions.py", line 7, in <module>
for step in direction['Direction']['Routes'][0]['Steps']:
TypeError: list indices must be integers, not str

【问题讨论】:

  • 那么您认为这里有什么问题?
  • 我不知道为什么会出现错误

标签: python google-maps-api-2


【解决方案1】:

我无法帮助您直接查明错误,因为我没有 API 密钥来亲自尝试。不过,我可以给你一些东西,你可以尝试并考虑找出问题的根源。

您的 for 循环 (for step in direction['Direction']['Routes'][0]['Steps']:) 中的索引之一似乎应该是整数。

您可以尝试使用内置的type() 函数找出哪个。所以要调试,我会尝试:type(direction),然后是type(direction['Direction']) 等等,direction['Direction']['Routes'] 除外,因为那已经是一个列表。

我建议您为此使用交互式 python 解释器。您要寻找的是当您输入代码(例如type(direction))时,您会得到type&lt;'list'&gt;

在找出罪魁祸首之后,也许您可​​以对其进行索引并找到您想要的那个或阅读Google Docs

【讨论】:

  • 能否请您详细解释一下,因为我试过了,但没有任何反应
  • @RishavGupta 不应该发生任何事情。这只是一种调试方法。使用 python 解释器找出你的direction['Direction']['Routes'][0]['Steps'] 的哪一部分是错误的。
猜你喜欢
  • 1970-01-01
  • 2016-11-30
  • 1970-01-01
  • 1970-01-01
  • 2012-05-09
  • 1970-01-01
  • 1970-01-01
  • 2015-06-13
  • 2020-04-05
相关资源
最近更新 更多