2016-10-18 11 views
-1

「更新」GoogleのマトリックスAPI - PythonのリターンNonetypeエラー

*最後に、問題を解決はTypeErrorが含まれており、また以外に渡す使用の代わりに、継続する以外試みを変更しました。

「アップデートの終了」

私はGoogleの距離行列のAPIを使用して2つの位置の間の距離を検索するためのコードを書きました。原点は固定ですが、目的地はxlsxファイルから取得します。私はキーのような目的地の辞書を取得し、距離はと期待していました。以下のコードを実行すると、特定のループの後に私はこのエラーコードに遭遇しました。

TypeError: Expected a lat/lng dict or tuple, but got NoneType 

エラーの原因を理解できますか?ここでは、コード(pygmap.py)です:

import googlemaps 
import openpyxl 

#get origin and destination locations 
def cleanWB(file_path): 
    destination = list() 

    wb = openpyxl.load_workbook(filename=file_path) 
    ws = wb.get_sheet_by_name('Sheet1') 
    for i in range(ws.max_row): 
     cellValueLocation = ws.cell(row=i+2,column=1).value 
     destination.append(cellValueLocation) 

    #remove duplicates from destination list 
    unique_location = list(set(destination)) 
    return unique_location 

def getDistance(origin, destination): 
    #Google distance matrix API key 
    gmaps = googlemaps.Client(key = 'INSERT API KEY') 
    distance = gmaps.distance_matrix(origin, destination) 
    distance_status = distance['rows'][0]['elements'][0]['status'] 
    if distance_status != 'ZERO_RESULTS': 
     jDistance = distance['rows'][0]['elements'][0] 
     distance_location = jDistance['distance']['value'] 
    else: 
     distance_location = 0 

    return distance_location 

は、私はこのコードを使用して、それを実行します。

import pygmap 

unique_location = pygmap.cleanWB('C:/Users/an_id/Documents/location.xlsx') 
origin = 'alam sutera' 
result = {} 
for i in range(len(unique_location)): 
    try: 
     result[unique_location[i]] = pygmap.getDistance(origin, unique_location[i]) 
    except (KeyError, TypeError): 
     pass 

私は結果を印刷する場合、それは私が成功した46件の結果

を取得していることが表示されます

結果 { 'ポンドックピナン':25905、 'Jatinegara Kaum':40453、 'Serdang':1623167、「Jatiasih ':44737、' Tanah Sereal ':77874、' Jatikarya ':48399、' Duri Kepa ':20716、' Mampan g Prapatan ':31880、' Pondok Pucung ':12592、' Johar Baru ':46791、' Karet ':26889、 ' Bukit Duri ':34039、' Sukamaju ':55333、' Pasir Gunung Selatan ':42140、' Pinangs ia ':30471、' Pinang Ranti ':38099、' Bantar Gebang ':50778、'スカブミ「Kambangan Utara」:17708、「Kwitang」:25860、「Kuningan Barat」:31231、「Cilodo ng」:58879、「Pademangan Barat」:32585、「Kebon Kelapa」:23452、メッカ・ジャヤ ':5381 0、'カンポン・バリ ':1188894、'パジャン ':30008、'スカジャジュ・バル ':53708、'ベンダ・バルー ':19965、'スカブミ・セレラ ':19095、'ガンダリア・ウタラ ':28429、 'Setia Mulya':635 34、 'Rawajati':31724、 'Cireundeu':28220、 'Cimuning':55712、 'Lebak Bul私たちは、私たちの生活の中で、私たちの生活の中で最も重要なのです。 'Bojongサリバル':26978、 'Padurenan':53216、 'ジャティMekar':2594703、 'Jatirang GA':51119}

答えて

0

Try ExceptTypeErrorを含むように問題を解決しました。そしてまた、私もこのソリューションを使用して、いくつかの場所をスキップpass代わりのcontinue

import pygmap 

unique_location = pygmap.cleanWB('C:/Users/an_id/Documents/location.xlsx') 
origin = 'alam sutera' 
result = {} 


#get getPlace 
for i in range(len(unique_location)): 
    try: 
     result[unique_location[i]] = pygmap.getDistance(origin, unique_location[i]) 
    except (KeyError, TypeError): 
     pass 

使用しています。

関連する問題