如何使用Python从多个来源到多个目的地计算谷歌距离矩阵API 这是我的代码,外循环有问题。你能帮我解决一下吗?
import googlemaps
import csv
gmaps = googlemaps.Client(key='My_google_distance_KEY')
count1 = 1
count2 = 1
Name_index = 2
station_index = 14
infile1 = open('C:\Users\Thinkpad\Documents\Python Scripts\Input\Metro1.csv', 'r')
infile2 = open('C:\Users\Thinkpad\Documents\Python Scripts\Input\Metro2.csv', 'r')
output1 = open('C:\Users\Thinkpad\Documents\Python Scripts\Output\M_distance.csv', 'w')
reader1 = csv.reader(infile1)
reader2 = csv.reader(infile2)
writer1 = csv.writer(output1)
for row in list(reader1)[count1:]:
Name1 = row[Name_index]
Location1 = row[station_index]
print(Location1)
for row in list(reader2)[count2:]:
Location2 = row[station_index]
Name2 = row[Name_index]
my_distance = gmaps.distance_matrix(Location1, Location2, mode='transit', language=None, avoid=None, units=None,
departure_time='1475925955', arrival_time=None,
transit_routing_preference=None)
print(my_distance)
if my_distance['rows'][0]['elements'][0]['status'] != 'ZERO_RESULTS':
Duration = my_distance['rows'][0]['elements'][0]['duration']['value']
else:
Duration = 10000000
if my_distance['rows'][0]['elements'][0]['status'] != 'ZERO_RESULTS':
Distance = my_distance['rows'][0]['elements'][0]['distance']['value']
else:
Distance = 10000000
writer1.writerow((Name1, Name2, Duration, Distance,))
结果如下:
39.72327906 , 116.1253664
{u'status': u'OK', u'rows': [{u'elements': [{u'duration': {u'text': u'1 min', u'value': 0}, u'distance': {u'text': u'1 m', u'value': 0}, u'status': u'OK'}]}], u'origin_addresses': [u'55 Chang Hong Xi Lu, Fangshan Qu, Beijing Shi, China'], u'destination_addresses': [u'55 Chang Hong Xi Lu, Fangshan Qu, Beijing Shi, China']}
{u'status': u'OK', u'rows': [{u'elements': [{u'duration': {u'text': u'17 mins', u'value': 1029}, u'distance': {u'text': u'1.4 km', u'value': 1376}, u'status': u'OK'}]}], u'origin_addresses': [u'55 Chang Hong Xi Lu, Fangshan Qu, Beijing Shi, China'], u'destination_addresses': [u'Chang Hong Xi Lu, Fangshan Qu, Beijing Shi, China, 102401']}
{u'status': u'OK', u'rows': [{u'elements': [{u'duration': {u'text': u'22 mins', u'value': 1327}, u'distance': {u'text': u'2.9 km', u'value': 2871}, u'status': u'OK'}]}], u'origin_addresses': [u'55 Chang Hong Xi Lu, Fangshan Qu, Beijing Shi, China'], u'destination_addresses': [u'9 Chang Hong Dong Lu, Fangshan Qu, Beijing Shi, China, 102401']}
{u'status': u'OK', u'rows': [{u'elements': [{u'duration': {u'text': u'23 mins', u'value': 1369}, u'distance': {u'text': u'4.4 km', u'value': 4436}, u'status': u'OK'}]}], u'origin_addresses': [u'55 Chang Hong Xi Lu, Fangshan Qu, Beijing Shi, China'], u'destination_addresses': [u'Chang Hong Dong Lu, Fangshan Qu, Beijing Shi, China']}
{u'status': u'OK', u'rows': [{u'elements': [{u'duration': {u'text': u'25 mins', u'value': 1500}, u'distance': {u'text': u'5.6 km', u'value': 5616}, u'status': u'OK'}]}], u'origin_addresses': [u'55 Chang Hong Xi Lu, Fangshan Qu, Beijing Shi, China'], u'destination_addresses': [u'Dong Huan Lu, Fangshan Qu, Beijing Shi, China']}
39.72328283 , 116.1408152
39.72325725 , 116.1562973
39.7232697 , 116.1764466
39.729987 , 116.183392
应打印5 * 5行距离。现在它只有5行。
如果可能的话,你会帮我解决一下吗?
最佳, 国卫