【发布时间】:2021-07-02 02:30:22
【问题描述】:
import requests
import json
response = requests.get('http://dataservice.accuweather.com/currentconditions/v1/2807435?apikey=secret')
print(response)
x = response.json()
y = json.dumps(x)
z = json.loads(y)
a = z['WeatherText']
print(a)
yt = input("Press Enter to close")
此代码用于从 accuweather 检索天气数据。当我运行文件时,我得到一个错误:
TypeError: 列表索引必须是整数或切片,而不是 str
初始 json 文件的示例图像是: This image is the output
请帮我解决错误
【问题讨论】:
-
请使用完整的错误回溯更新您的问题。
-
顺便说一句,我想你只需要
x。y和z只是在复制您在x中已有的内容。 -
貌似
z是dicts的列表,不能用字符串索引列表,必须先提取dict