Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何修复';键错误';在处理api响应搜索时?_Python_Json_Api_Request_Response - Fatal编程技术网

Python 如何修复';键错误';在处理api响应搜索时?

Python 如何修复';键错误';在处理api响应搜索时?,python,json,api,request,response,Python,Json,Api,Request,Response,我正在创建一个天气应用程序。我试图从API中提取天气数据。执行从列表检索时,我收到一个keyrerror。我模糊了我的API密钥 我试着将密钥设置为多个不同的数字,然后将其完全删除max'是api的一部分 city = 'Syracuse' state = 'NY' url = ('https://api.openweathermap.org/data/2.5/forecast?q=%s,3166-2:US-%s&APPID=**********' % (city,state)) res

我正在创建一个天气应用程序。我试图从API中提取天气数据。执行从列表检索时,我收到一个keyrerror。我模糊了我的API密钥

我试着将密钥设置为多个不同的数字,然后将其完全删除max'是api的一部分

city = 'Syracuse'
state = 'NY'
url = ('https://api.openweathermap.org/data/2.5/forecast?q=%s,3166-2:US-%s&APPID=**********' % (city,state))
response = requests.get(url)
weatherdata = response.json()
weatherdata
low = weatherdata[0]['temp_min']
错误出现在第7行并读取

KeyError                                  Traceback (most recent call last)
<ipython-input-52-18029d6f472d> in <module>
     25 weatherdata
     26 
---> 27 low = weatherdata[0]['temp_min']

KeyError: 0

我希望将一天的最高温度设置为“高”,并且我能够在一天的低温度、湿度和其他条件下重复此过程。因此,不是错误,而是将其设置为高值,然后我可以稍后打印或使用它。

很明显,响应返回一个json数组,其中索引0处的字典没有名为
max
的键。打印天气时的输出是什么?

API:

代码

输出

JSON Response : {u'city': {u'name': u'Moscow', u'country': u'RU', u'lon': 37.6156, u'geoname_id': 524901, u'iso2': u'RU', u'lat': 55.7522, u'type': u'city', u'population': 0}, u'message': 0, u'list': [{u'clouds': 0, u'temp': {u'min': 261.41, u'max': 262.65, u'eve': 262.65, u'morn': 262.65, u'night': 261.41, u'day': 262.65}, u'snow': 0.01, u'humidity': 76, u'pressure': 1024.53, u'weather': [{u'main': u'Clear', u'id': 800, u'icon': u'01d', u'description': u'sky is clear'}], u'dt': 1485766800, u'speed': 4.57, u'deg': 225}, {u'clouds': 88, u'temp': {u'min': 260.98, u'max': 265.44, u'eve': 264.18, u'morn': 261.46, u'night': 265.44, u'day': 262.31}, u'snow': 1.44, u'humidity': 91, u'pressure': 1018.1, u'weather': [{u'main': u'Snow', u'id': 600, u'icon': u'13d', u'description': u'light snow'}], u'dt': 1485853200, u'speed': 4.1, u'deg': 249}, {u'clouds': 64, u'temp': {u'min': 266.9, u'max': 270.59, u'eve': 269.66, u'morn': 266.9, u'night': 268.06, u'day': 270.27}, u'snow': 0.92, u'humidity': 92, u'pressure': 1010.85, u'weather': [{u'main': u'Snow', u'id': 600, u'icon': u'13d', u'description': u'light snow'}], u'dt': 1485939600, u'speed': 4.53, u'deg': 298}, {u'clouds': 0, u'temp': {u'min': 255.19, u'max': 264.02, u'eve': 259.68, u'morn': 263.38, u'night': 255.59, u'day': 263.46}, u'humidity': 84, u'pressure': 1019.32, u'weather': [{u'main': u'Clear', u'id': 800, u'icon': u'01d', u'description': u'sky is clear'}], u'dt': 1486026000, u'speed': 3.06, u'deg': 344}, {u'clouds': 45, u'temp': {u'min': 256.55, u'max': 266, u'eve': 260.09, u'morn': 266, u'night': 256.55, u'day': 265.69}, u'snow': 0.21, u'humidity': 0, u'pressure': 1012.2, u'weather': [{u'main': u'Snow', u'id': 600, u'icon': u'13d', u'description': u'light snow'}], u'dt': 1486112400, u'speed': 7.35, u'deg': 24}, {u'clouds': 29, u'temp': {u'min': 254.73, u'max': 259.95, u'eve': 254.73, u'morn': 257.02, u'night': 257.13, u'day': 259.95}, u'humidity': 0, u'pressure': 1029.5, u'weather': [{u'main': u'Clear', u'id': 800, u'icon': u'01d', u'description': u'sky is clear'}], u'dt': 1486198800, u'speed': 2.6, u'deg': 331}, {u'clouds': 46, u'temp': {u'min': 259.11, u'max': 263.13, u'eve': 261.32, u'morn': 259.11, u'night': 262.01, u'day': 263.13}, u'snow': 0.04, u'humidity': 0, u'pressure': 1023.21, u'weather': [{u'main': u'Snow', u'id': 600, u'icon': u'13d', u'description': u'light snow'}], u'dt': 1486285200, u'speed': 5.33, u'deg': 234}], u'cod': u'200', u'cnt': 7}
max 262.65

您可以只打印JSON响应吗?错误状态
weatherdata
不是JSON数组。是的,我可以打印JSON响应。然而,它太长,不容易阅读。我将进行编辑以包含that.import json,并在python中使用json.dumps(您的_json_响应)以可读格式打印json响应。json已导入,而我目前使用的格式更可读。我在上面打印的json中看不到任何键“max”,而且从API获得的响应是json对象,不是JSON数组。即使我尝试“weather=weatherdata[0][weather]”也一样,非常感谢。我被这个答案弄糊涂了,但我尝试了几种方法,使它起了作用。
import requests

url = ('https://samples.openweathermap.org/data/2.5/forecast/daily?id=524901&lang=us&appid=b6907d289e10d714a6e88b30761fae22')
response = requests.get(url)
weatherdata = response.json()

print "JSON Response :", weatherdata
high = weatherdata['list'][0]['temp']['max']
print "max", max
JSON Response : {u'city': {u'name': u'Moscow', u'country': u'RU', u'lon': 37.6156, u'geoname_id': 524901, u'iso2': u'RU', u'lat': 55.7522, u'type': u'city', u'population': 0}, u'message': 0, u'list': [{u'clouds': 0, u'temp': {u'min': 261.41, u'max': 262.65, u'eve': 262.65, u'morn': 262.65, u'night': 261.41, u'day': 262.65}, u'snow': 0.01, u'humidity': 76, u'pressure': 1024.53, u'weather': [{u'main': u'Clear', u'id': 800, u'icon': u'01d', u'description': u'sky is clear'}], u'dt': 1485766800, u'speed': 4.57, u'deg': 225}, {u'clouds': 88, u'temp': {u'min': 260.98, u'max': 265.44, u'eve': 264.18, u'morn': 261.46, u'night': 265.44, u'day': 262.31}, u'snow': 1.44, u'humidity': 91, u'pressure': 1018.1, u'weather': [{u'main': u'Snow', u'id': 600, u'icon': u'13d', u'description': u'light snow'}], u'dt': 1485853200, u'speed': 4.1, u'deg': 249}, {u'clouds': 64, u'temp': {u'min': 266.9, u'max': 270.59, u'eve': 269.66, u'morn': 266.9, u'night': 268.06, u'day': 270.27}, u'snow': 0.92, u'humidity': 92, u'pressure': 1010.85, u'weather': [{u'main': u'Snow', u'id': 600, u'icon': u'13d', u'description': u'light snow'}], u'dt': 1485939600, u'speed': 4.53, u'deg': 298}, {u'clouds': 0, u'temp': {u'min': 255.19, u'max': 264.02, u'eve': 259.68, u'morn': 263.38, u'night': 255.59, u'day': 263.46}, u'humidity': 84, u'pressure': 1019.32, u'weather': [{u'main': u'Clear', u'id': 800, u'icon': u'01d', u'description': u'sky is clear'}], u'dt': 1486026000, u'speed': 3.06, u'deg': 344}, {u'clouds': 45, u'temp': {u'min': 256.55, u'max': 266, u'eve': 260.09, u'morn': 266, u'night': 256.55, u'day': 265.69}, u'snow': 0.21, u'humidity': 0, u'pressure': 1012.2, u'weather': [{u'main': u'Snow', u'id': 600, u'icon': u'13d', u'description': u'light snow'}], u'dt': 1486112400, u'speed': 7.35, u'deg': 24}, {u'clouds': 29, u'temp': {u'min': 254.73, u'max': 259.95, u'eve': 254.73, u'morn': 257.02, u'night': 257.13, u'day': 259.95}, u'humidity': 0, u'pressure': 1029.5, u'weather': [{u'main': u'Clear', u'id': 800, u'icon': u'01d', u'description': u'sky is clear'}], u'dt': 1486198800, u'speed': 2.6, u'deg': 331}, {u'clouds': 46, u'temp': {u'min': 259.11, u'max': 263.13, u'eve': 261.32, u'morn': 259.11, u'night': 262.01, u'day': 263.13}, u'snow': 0.04, u'humidity': 0, u'pressure': 1023.21, u'weather': [{u'main': u'Snow', u'id': 600, u'icon': u'13d', u'description': u'light snow'}], u'dt': 1486285200, u'speed': 5.33, u'deg': 234}], u'cod': u'200', u'cnt': 7}
max 262.65