Python 为什么我的代码中有KeyError main?

Python 为什么我的代码中有KeyError main?,python,Python,我已经尝试了很多来解决这个问题,但是每次我都遇到同样的问题。主要城市天气的关键错误 import requests from django.shortcuts import render from .models import City from .forms import CityForm # Create your views here. def weather(request): url = 'http://api.openweathermap.org/data/2.5/we

我已经尝试了很多来解决这个问题,但是每次我都遇到同样的问题。主要城市天气的关键错误

import requests
from django.shortcuts import render
from .models import City
from .forms import CityForm

# Create your views here.

def weather(request):

    url = 'http://api.openweathermap.org/data/2.5/weather?q={}&units=imperial&appid=f5f13c3f6d997b396795738b674115cc'
    city = 'Delhi'


    if request.method == 'POST':
        form = CityForm(request.POST)
        form.save()

    form = CityForm()

    cities = City.objects.all()

    weather_data = []

    for city in cities:

        r = requests.get(url.format(city)).json()
        #print(r.text)
        city_weather = {
            'city': city.name,
            'temperature': r['main']['temp'],
            'description': r['weather'][0]['description'],
            'icon':  r['weather'][0]['icon']

        weather_data.append(city_weather)

    print(weather_data)

    #print(city_weather)
    context = {'weather_data': weather_data, 'form': form}
    return render(request, 'weather.html', context)
我没料到会有错误,但我得到了答案 “温度”:r['main']['temp'],
KeyError:'main'

每当您看到KeyError时,其语义是找不到要查找的键。
city_weather = {
            'city': city.name,
            'temperature': r['main']['temp'],
            'description': r['weather'][0]['description'],
            'icon':  r['weather'][0]['icon']
               } #you need to close the dictionary here

    weather_data.append(city_weather)

只需在字典“city\u weather

中加上结尾的花括号(“}”),每当你看到一个键错误时,语义上的意思是找不到要查找的键。
只需在字典“city_weather”的结尾处加上一个大括号(“}”)”

当你打印(r)?“{”坐标“:{”lon“:77.22,“lat“:28.65}”,“weather:[{”id“:721,“main:”Haze“,”description“:”Haze“,”icon“:”50d“}”,“base:”stations“,”main:“{”temp“:”92.28,“pressure”:1003,“湿度“:”59,“temp\u min”:91.4,“temp_max”:93.2},“能见度”:3500,“风”:{“速度”:3.36,“度”:90},“云”:{“全部”:40},“dt”:1563517445,“系统”:{“类型”:1,“id”:9161,“信息”:0.0072,“国家”:“在”,“日出”:1563494686,“日落”:1563544175},“时区”:19800,“id”:1273294,“名称”:“德里”,“鳕鱼”:200}似乎不错。也许r是一个字符串(因为您将其jsonified)。尝试
print(type(r))
,如果不是
dict
则删除将词典转换为其stringStarting development server的
.json()
,然后使用CTRL-BREAK退出服务器温度:r['main']['temp'],keyrerror:'main'[19/Jul/2019 16:07:12]“GET/HTTP/1.1”500 65284一切正常。。。。对不起,可能是
r.main
,但我不这么认为。调试
导入pdb的时间;pdb.设置_trace()
然后播放r。祝你好运,永不放弃!当你打印(r)时,是否有一个叫做“main”的键?{“coord”:{“lon”:77.22,“lat”:28.65},“weather”:[{“id”:721,“main”:“Haze”,“description”:“Haze”,“icon”:“50d”),“base”:“stations”,“main”:{“temp”:92.28,“pressure”:1003,“湿度”:59,“temp(min”:91.4,“temp)max”:93.2},“temp(max”:3500,“wind”:“风速”:3.36”,“deg”:90},“云层”:“能见度”40}”:1563517445,“sys”:{“type”:1,“id”:9161,“message”:0.0072,“country”:“IN”,“sunrise”:156344686,“sunset”:1563544175},“timezone”:19800,“id”:1273294,“name”:“德里”,“cod”:200}看起来不错。也许r是一个字符串(因为您将其jsonified)。尝试
print(type(r))
,如果不是
dict
则删除将词典转换为其stringStarting development server的
.json()
,然后使用CTRL-BREAK退出服务器温度:r['main']['temp'],keyrerror:'main'[19/Jul/2019 16:07:12]“GET/HTTP/1.1”500 65284一切正常。。。。对不起,可能是
r.main
,但我不这么认为。调试
导入pdb的时间;pdb.设置_trace()
然后播放r。祝你好运,永不放弃!url='{}&units=imperial&appid=F5F13C3F6D997B3967995738B674115CC'然后这里是一个问题或者你可以尝试将这个'city':city.name更改为'city':r['name'],我将'city':city.name改为'city':r['name']。。。。。“city”再次出现关键错误:r['name']url='{}&units=imperial&appid=f5f13c3f6d997b396795738b674115cc'然后问题是,您可以尝试将此'city':city.name改为'city':r['name',我将'city':city.name改为'city':r['name'。。。。。再次获得“city”的键错误:r['name']