我正在嘗試從https://fantasy.premierleague.com/drf/event/19/live 上的 JSON 文件中獲取數(shù)據(jù)。但是,每當(dāng)我遍歷接收到的數(shù)據(jù)時(shí),我都會(huì)遇到問題。下面代碼中的第一個(gè)循環(huán)應(yīng)該在 中附加值data["elements"]["1"]["stats"]["goals_scored"],但是每當(dāng)我運(yùn)行下面的代碼時(shí),我都會(huì)收到錯(cuò)誤字符串索引必須是整數(shù)。每當(dāng)我data["elements"]["number"]["stats"]["goals_scored"]直接引用時(shí),它都會(huì)返回正確的數(shù)字。import jsonimport requestsdef goalCalculator(): data=requests.get("https://fantasy.premierleague.com/drf/event/19/live").json() list1=[] for i in data["elements"]: list1.append(i["stats"]["goals_scored"]) return list1goalCalculator()我已經(jīng)映射了 JSON 文件(如下),但我看不出哪里出錯(cuò)了。我的代碼不應(yīng)該工作正常嗎?"elements":{ "1":{ "stats":{ "yellow_cards":0, "own_goals":0, "creativity":0.0, "goals_conceded":0, "bonus":0, "red_cards":0, "saves":0, "influence":0.0, "bps":0, "clean_sheets":0, "assists":0, "ict_index":0.0, "goals_scored":0, "threat":0.0, "penalties_missed":0, "total_points":0, "penalties_saved":0, "in_dreamteam":false, "minutes":0 } }, "2":{etc...
循環(huán)遍歷嵌套字典
繁星點(diǎn)點(diǎn)滴滴
2021-09-28 14:58:23