Google Matrix API - python返回Nonetype错误

时间:2016-10-18 04:48:15

标签: python-3.x google-maps-api-3 google-distancematrix-api

“更新”

*最后解决了这个问题,更改了try除了包含TypeError,并且在except中使用pass而不是continue。

“更新结束”

我编写了代码,使用Google Distance Matrix API搜索两个位置之间的距离。原始位置是固定的,但是对于目的地,我从xlsx文件中获取它。我希望将带有Destination的Dictionary作为 Key ,将距离作为。当我运行下面的代码时,在某个循环后我偶然发现了这个错误代码:

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个结果

  
    
      

结果           {'Pondok Pinang':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,'Sukabumi Utara':204           41,'Kembangan Utara':17708,'Kwitang':25860,'Kuningan Barat':31231,'Cilodo           ng':58879,'Pademangan Barat':32585,'Kebon Kelapa':23452,'Mekar Jaya':5381           0,'Kampung Bali':1188894,'Pajang':30008,'Sukamaju Baru':53708,'Benda Baru           ':19965,'Sukabumi Selatan':19095年,'Gandaria Utara':28429,'Setia Mulya':635           34,'Rawajati':31724,'Cireundeu':28220,'Cimuning':55712,'Lebak Bulus':273           61,'Kayuringin Jaya':47560,'Kedaung Kali Angke':19171,'Pagedangan':16791,           'Karang Anyar':171165,'Petukangan Selatan':18959,'Rawabadak Selatan':42765,            'Bojong Sari Baru':26978,'Padurenan':53216,'Jati Mekar':2594703,'Jatirang           ga':51119}

    
  

1 个答案:

答案 0 :(得分:0)

解决了在TypeError中加入Try Except的问题。并使用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

我使用此解决方案跳过了一些位置。