-1
我试图从json urlfile打印'temp'项目。问题是,我不能只需键入print(item['temp']
如果我这样做,我得到这个错误:无法打印来自json api的想要的项目
TypeError: string indices must be integers
所以我想我不得不使用一个整数,而不是,这是我做的。但是,当它 打印出结果,我得到这个:
“F C”
这没有任何意义,无所谓我输入的内容数量,我得到的形式结果2个字母。
下面的代码:
import urllib.request
import json
url = urllib.request.urlopen("http://www.myweather2.com/developer/forecast.ashx?uac=fRnJKj3Xpi&output=json&query=SW1")
readurl = url.read()
json_obj = str(readurl, "utf-8")
data = json.loads(json_obj)
for item in data['weather']:
print(item[0])
这是因为'item'是一个字符串,而不是字典。他们是字典中的*键*。 –
@MartijnPieters如何在这样的列表中调用一个项目? – Vitalynx