我试图从googlemapsapi获取一些关于行程的距离和持续时间的信息。问题是我试图找到从某个城市到其他城市的路线,在某些情况下,那里没有现有的道路。在这种情况下,谷歌地图说他们没有结果。没关系,但我是python新手,所以我现在不知道如何忽略这个错误,继续计算其余的行程。在
gmaps = googlemaps.Client(key='API_KEY')
Indice_Ciudad=0
input=open(r'C:\Users\SantiagoCC\Desktop\destinos.csv','r')
output=open(r'C:\Users\SantiagoCC\Desktop\Distancias_a_Corabastos.csv','w')
try:
reader=csv.reader(input)
writer=csv.writer(output)
for row in list(reader)[1:]:
ciudad=row[Indice_Ciudad]
print(ciudad)
distancias=gmaps.distance_matrix((4.630562, -74.159983), ciudad)
writer.writerow((ciudad,
distancias['rows'][0]['elements'][0]['distance']['value'],
distancias['rows'][0]['elements'][0]['duration']['value'], ))
当行程不存在时,它只显示keyror'distance'。在
你可以捕捉到异常并相应地处理它。请注意,这将包括其他关键错误(不仅仅是在距离字段)。在
相关问题 更多 >
编程相关推荐