在Python中使用Google距离矩阵API时出错

里沙夫·古普塔(Rishav Gupta)

这是我的代码:

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
月亮·芝士(Moon Cheesez)

由于我没有API密钥可以自己尝试,因此我将无法帮助您直接查明错误。但是,我可以为您提供一些您可以尝试并考虑找到问题根源的方法。

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

您可以尝试使用内置type()功能找出哪个因此,要调试,我会尝试:type(direction),然后type(direction['Direction'])以此类推,direction['Direction']['Routes']因为那已经是一个列表了。

我建议您为此使用交互式python解释器。您要查找的是输入代码(例如type(direction))时得到的代码type<'list'>

在找出哪个罪魁祸首之后,也许您可​​以对它进行索引,找到想要的罪魁祸首,或者阅读Google文档

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章