字符串格式化python KeyError

时间:2014-06-10 13:37:25

标签: python string kivy

以下代码中list字符串的功能是什么:

cities = ['{} ({})'.format(d['name'],d['sys']['country']) for d in data['list']]

我在问,因为在方法found_location_name中它起作用

def found_location_name(self, request, data):
    data = json.loads(data.decode()) if not isinstance(data, dict) else data
    cities = ['{} ({})'.format(d['name'],d['sys']['country']) for d in data['list']]
    self.search_results.item_strings = cities

在这里我得到KeyError:

def found_location_coordinates(self,request_coordinates,data_coordinates):
    data_coordinates = json.loads(data_coordinates.decode()) if not isinstance(data_coordinates, dict) else data_coordinates 
    cities_coordinates = ['{} ({})'.format(d['name'],d['sys']['country']) for d in data_coordinates['list']]
    self.search_results.item_strings = cities_coordinates


Traceback (most recent call last):
File "main.py", line 39, in <module>
 WeatherApp().run()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/Kivy-1.8.0-py2.7-macosx-10.6-intel.egg/kivy/app.py", line 792, in run
 runTouchApp()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/Kivy-1.8.0-py2.7-macosx-10.6-intel.egg/kivy/base.py", line 481, in runTouchApp
 EventLoop.window.mainloop()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/Kivy-1.8.0-py2.7-macosx-10.6-intel.egg/kivy/core/window/window_pygame.py", line 381, in mainloop
 self._mainloop()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/Kivy-1.8.0-py2.7-macosx-10.6-intel.egg/kivy/core/window/window_pygame.py", line 287, in _mainloop
 EventLoop.idle()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/Kivy-1.8.0-py2.7-macosx-10.6-intel.egg/kivy/base.py", line 321, in idle
 Clock.tick()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/Kivy-1.8.0-py2.7-macosx-10.6-intel.egg/kivy/clock.py", line 422, in tick
 self._process_events()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/Kivy-1.8.0-py2.7-macosx-10.6-intel.egg/kivy/clock.py", line 537, in _process_events
 if event.tick(self._last_tick) is False:
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/Kivy-1.8.0-py2.7-macosx-10.6-intel.egg/kivy/clock.py", line 309, in tick
 ret = callback(self._dt)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/Kivy-1.8.0-py2.7-macosx-10.6-intel.egg/kivy/network/urlrequest.py", line 376, in _dispatch_result
 func(self, data)
File "main.py", line 26, in found_location_coordinates
 cities_coordinates = ['{} ({})'.format(d['name'],d['sys']['country']) for d in data_coordinates['list']]
KeyError: 'list'

1 个答案:

答案 0 :(得分:3)

data['list']表示data是字典,您需要list键的值。因此,data可能如此:

data = {
    'list': [1, 2, 3, 4, 5]
}

然后,

>>> data['list']
[1, 2, 3, 4, 5]

现在,如果您尝试访问不存在的密钥,则会引发KeyError

>>> data['qwerty']
<omitted traceback>
KeyError: 'qwerty'

因此,data_coordinates字典没有list密钥。如果data_coordinates有时可能有list密钥,有时却没有,您可以使用返回None的{​​{3}}方法或指定的默认值(如果密钥没有存在:

>>> data.get('qwerty')
None
>>> data.get('qwerty', 'default value')
'default value'