Python 尝试将字典中的键分配给变量-列表索引必须是整数或片,而不是str

Python 尝试将字典中的键分配给变量-列表索引必须是整数或片,而不是str,python,json,api,typeerror,openweathermap,Python,Json,Api,Typeerror,Openweathermap,我尝试在python中使用RESTAPI。当我试图从使用jason数据创建的字典中分配密钥时,发生了此错误。我是python新手,我找不到解决这个问题的方法 import requests from pprint import pprint lokka = str(input("What is the location you need information of?")) #takes the location as "lokka" hellload = requests.get("htt

我尝试在python中使用RESTAPI。当我试图从使用jason数据创建的字典中分配密钥时,发生了此错误。我是python新手,我找不到解决这个问题的方法

import requests
from pprint import pprint

lokka = str(input("What is the location you need information of?"))
#takes the location as "lokka"

hellload = requests.get("http://api.openweathermap.org/data/2.5/weather?q="+ lokka +"&appid=xxxxxxxxxxxxxxxxx&units=metric")
#the rest api's load will be taken to the account of hellload

jputha = hellload.json()
#json data will be converted to a dictionary
#print (jputha)

#---------------------------------------------------------
#from now onward I'll be kickin the hell out the jsons
long = str(jputha["coord"]["lon"])
lat = str(jputha["coord"]["lat"])
wthr = str(jputha["weather"]["main"])
temp = str(jputha["main"]["temp"])
winspd = str(jputha["wind"]["speed"])

print(long)
print(lat)
print(wthr)
print(temp)
print(winspd)

根据OpenWeatherMap,API的JSON响应如下所示:

{"coord":
{"lon":145.77,"lat":-16.92},
"weather":[{"id":803,"main":"Clouds","description":"broken clouds","icon":"04n"}],
"base":"cmc stations",
"main":{"temp":293.25,"pressure":1019,"humidity":83,"temp_min":289.82,"temp_max":295.37},
"wind":{"speed":5.1,"deg":150},
"clouds":{"all":75},
"rain":{"3h":3},
"dt":1435658272,
"sys":{"type":1,"id":8166,"message":0.0166,"country":"AU","sunrise":1435610796,"sunset":1435650870},
"id":2172797,
"name":"Cairns",
"cod":200}
如果
weather
键包含dict列表,而不是dict,因此如果您只是想从列表中获取第一个天气数据,则应使用
[0]
获取第一个索引的值:

wthr = str(jputha["weather"][0]["main"])

您可以分享您遇到的错误吗?
jputha[“coord”]
返回与键
“coord”
关联的值,在本例中,该键似乎是一个列表。但是,列表不能由其元素索引,只能由整数索引。要进一步分析您的问题,您应该指定
lokka
用户输入。