【问题标题】:Whenever I try parsing JSON file I get KeyError in Python每当我尝试解析 JSON 文件时,我都会在 Python 中得到 KeyError
【发布时间】:2015-11-15 01:33:29
【问题描述】:

我正在使用 Google 地图 API 这是我的代码:

import googlemaps
import urllib2
import json

apiKey = '****************************'
url = 'https://maps.googleapis.com/maps/api/directions/json?departure_time=now&origin='
origin = raw_input("Origin ")
destination = raw_input("Destination ")
finalUrl = url + origin + '&destination=' + destination + '&key=' + apiKey

json_obj = urllib2.urlopen(finalUrl)
data = json.load(json_obj)

for item in data['routes']:
    print item['duration_in_traffic']

这是我得到的错误:

Traceback(最近一次调用最后一次):

文件“C:/Users/Documents/python/map2.py”,第 15 行,在

打印项目['duration_in_traffic']

KeyError: 'duration_in_traffic'

我已经浏览了 JSON 文件,并且该密钥肯定在那里。

这是物品

{u'routes': [{u'overview_polyline': {u'points': u'cxuyH_|Mf@bB~BsB?A?A?C@IYyAOw@EAGKCQ@I@A_@[OYU_@]eAoAeEaBsHk@gCu@uC_@oAWk@Y]SM_Aa@iAe@_F}@oDk@q@GgAAoBHwAHiCPoAHsA @o@@aDd@g@LkAHs@AgC[s@CoA[kAKEqBKsAw@gFwBcNoAmJg@_F'}, u'warnings': [], u'bounds': {u'northeast': {u'lat': 51.5752274, u'lng':0.09127539999999999},u'southwest':{u'lat':51.5615695, u'lng': 0.0758157}}, u'waypoint_order': [], u'summary': u'Ley St', u'copyrights': u'地图数据\xa92015 Google', u'legs': [{u'distance': {u'text': u'2.3 km', u'value': 2293}, u'end_address': u'Ilford, Ilford, Greater London IG2, UK', u'via_waypoint': [], u'duration_in_traffic': {u'text': u'5 mins', u'value': 315}, u'start_address': u'Ilford, Ilford, Greater London IG1, UK', u'start_location':{u'lat':51.5624203,u'lng':0.0763182},u'steps': [{u'html_instructions': u'Head southwest on Thorold RdAdelaide Rd', u'distance': {u'text': u'42 m', u'value': 42}, u'travel_mode': u'DRIVING', u'start_location': {u'lat': 51.5624203, u'lng': 0.0763182}, u'polyline': {u'points': u'cxuyH_|Mb@zABF'}, u'duration': {u'text': u'1 min', u'值':6}, u'end_location': {u'lat': 51.5622154, u'lng': 0.0758157}}, {u'html_instructions': u'在Adelaide Rd左转', u'distance': {u'text': u'82 m', u'value': 82}, u'travel_mode': u'DRIVING', u'maneuver': u'turn-left', u'start_location': {u'lat': 51.5622154, u'lng': 0.0758157}, u'polyline': {u'points': u'{vuyH{xM~BsB'}, u'duration': {u'text': u'1 min', u “价值”:33}, u'end_location':{u'lat':51.56157770000001,u'lng':0.0763982}}, {u'html_instructions': u'在环岛,从1st出口 到 Hainault St', u'distance': {u'text': u'62 m', u'value': 62}, u'travel_mode': u'DRIVING', u'maneuver': u'roundabout-left', u'start_location':{u'lat':51.56157770000001,u'lng':0.0763982}, u'polyline': {u'points': u'{ruyHo|M?A?A?A?A?A?A?A?A@??AU_ACYKw@'}, u'duration': {u'text': u'1 min', u'value': 16}, u'end_location': {u'lat': 51.5617618, u'lng': 0.0772199}}, {u'html_instructions': u'At 环岛,从 1st 出口驶入 Ley St', u'distance': {u'text': u'1.5 km', u'value': 1539}, u'travel_mode': u'DRIVING', u'maneuver': u'roundabout-left', u'start_location': {u'lat':51.5617618,u'lng':0.0772199},u'polyline':{u'points': u'tuyHsaNA?A?A?A??AA?AAA??AAA?AA??A?AAA?A?A?A?AAA?A?A?A?A@??A?A? A?A@A@[OYU_@]eAoAeEaBsHYwAQo@?Eu@oCUw@IWIUMUEKSQKIGCq@[MEiAe@F}@KAcDi@q@GgAAsAD[Bi@Bm@D{@HmAFoAHsA@o@@ aDd@G@@JcAFG@s@AgC[s@CoA[i@G'}, u'duration': {u'text': u'4 mins', u'value': 263}, u'end_location': {u'lat': 51.5734933, u'lng': 0.08373649999999999}}, {u'html_instructions': u'转东 Ave/A12目的地将在 左边', u'距离': {u'text': u'0.6 km', u'value': 568}, u'travel_mode': u'DRIVING', u'maneuver': u'turn-right', u'start_location':{u'lat':51.5734933,u'lng':0.08373649999999999}, u'polyline': {u'points': u'i}wyHkjOa@CA}@Cs@Eq@Ea@EYYqBSmACMMw@SsA]yBw@}Ey@eGGg@E_@G_@[mCKqA'}, u'duration': {u'text': u'1 min', u'value': 81}, u'end_location': {u'lat':51.5752274,u'lng':0.09127539999999999}}],u'duration': {u'text': u'7 mins', u'value': 399}, u'end_location': {u'lat': 51.5752274, u'lng': 0.09127539999999999}}]}], u'status': u'OK', u'geocoded_waypoints': [{u'place_id': u'ChIJA-CogdWl2EcRwTLERk1SnHY', u'geocoder_status': u'OK', u'types': [u'postal_code_prefix', u'postal_code']}, {u'place_id': u'ChIJSdgOJwWk2EcR_xRqH8zm13o', u'geocoder_status': u'OK', u'types': [u'postal_code_prefix', u'postal_code']}]}

【问题讨论】:

  • 您的代码已损坏。你确定那是你的代码吗?请发布完整的错误堆栈跟踪。
  • 代码有效。我只是不能让它显示 json 文件中的特定部分。
  • 这里的代码有语法错误,甚至可以从语法高亮中看到。而且它也不符合你的错误信息。
  • for item in data['routes]: 缺少结束引号。 print item[duration_in_traffic] 根本没有引号。无论如何,考虑打印出item 以查看字典中有哪些类型的键。因为显然,关键是 那里(Python 没有在这件事上撒谎)。
  • 我已在问题中添加了该项目。 'duration_in_traffic' 肯定存在。

标签: python json api google-maps keyerror


【解决方案1】:

item 不包含 duration_in_traffic 密钥。

对于您的示例输出,duration_in_traffic 值的正确路径是 item['legs'][0]['duration_in_traffic']

【讨论】:

  • 那么我该如何写出来而不是我现在拥有的呢?
  • 你用print item['legs'][0]['duration_in_traffic']代替print item['duration_in_traffic']
猜你喜欢
  • 2022-09-28
  • 2020-09-11
  • 2021-10-17
  • 2015-11-15
  • 1970-01-01
  • 2022-10-04
  • 2020-02-09
  • 2022-06-23
  • 1970-01-01
相关资源
最近更新 更多