2013-05-26 34 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

相關問題