2013-05-26 31 views
1

我是编程和python的新手,所以请不要讨厌。我正在尝试构建一个程序,它将excel表中的地址列表变成列表,然后使用google maps api检索每个地址的距离和时间。到目前为止,我只能硬编码它来做一个地址。 到目前为止,我唯一的问题是遍历gmaps.directions()参数。我会爱如何做到这一点任何帮助。 所以最后我想多次和距离来比较它们。由于如何运行迭代列表作为函数的参数 - googlemaps

from googlemaps import GoogleMaps 
import xlrd 



gmaps = GoogleMaps('gmaps api') 

wb = xlrd.open_workbook('addressbook.xlsx') 
ws1 = wb.sheet_by_name('Sheet1') 
ws2 = wb.sheet_by_name('Sheet2') 
col1 = ws1.col_values(1) 
col2 = ws2.col_values(1) 



dirs = gmaps.directions(col1[0] ,col2[0]) 
time = dirs['Directions']['Duration']['seconds'] 
dist = dirs['Directions']['Distance']['meters'] 



print time 
print dist 

回答

0

尽量坚持尽可能接近到什么你已经拥有:

from googlemaps import GoogleMaps 
import xlrd 



gmaps = GoogleMaps('gmaps api') 

wb = xlrd.open_workbook('addressbook.xlsx') 
ws1 = wb.sheet_by_name('Sheet1') 
ws2 = wb.sheet_by_name('Sheet2') 
col1 = ws1.col_values(1) 
col2 = ws2.col_values(1) 


for c1 in col1: 
    for c2 in col2: 
     dirs = gmaps.directions(c1 , c2) 
     time = dirs['Directions']['Duration']['seconds'] 
     dist = dirs['Directions']['Distance']['meters'] 

     print time 
     print dist 

(我不知道你是在总体规划如何利用新的,但不管它看起来像一个教程将按顺序)

+0

是的我想要学习python作为一种爱好。感谢你的帮助,我嫉妒你的知识。 – luketoboot

+0

如果我使用zip,那么我的两个excel表必须具有相同数量的地址。有没有另外的内置函数可以使用所有地址的排列? – luketoboot

+0

@luketoboot这更简单。在上面编辑。 zip(itertools.product)有一个类似的函数,但是双for循环可能更简单。 – U2EF1

相关问题