TypeError:将json转换为cs时,列表索引必须是整数,而不是dict

2024-04-18 23:07:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用python将保存在fpl.JSON文件中的JSON数据转换为CSV文件。我特别期待从文件中提取4块数据。第一名、第二名、总积分和事件积分。我已经包含了python代码

我得到以下错误

File "CreateCsv1.py", line 12, in pp = data [i] ["player_points"] TypeError: list indices must be integers, not dict

这是json数据:

[{"id":1,"photo":"11334.jpg","web_name":"Cech","team_code":3,"status":"a","code":11334,"first_name":"Petr","second_name":"Cech","squad_number":1,"news":"","now_cost":47,"news_added":"2018-09-29T17:31:14Z","chance_of_playing_this_round":100,"chance_of_playing_next_round":100,"value_form":"0.0","value_season":"5.1","cost_change_start":-3,"cost_change_event":0,"cost_change_start_fall":3,"cost_change_event_fall":0,"in_dreamteam":false,"dreamteam_count":0,"selected_by_percent":"1.1","form":"0.0","transfers_out":136211,"transfers_in":83497,"transfers_out_event":0,"transfers_in_event":0,"loans_in":0,"loans_out":0,"loaned_in":0,"loaned_out":0,"total_points":24,"event_points":0,"points_per_game":"3.4","ep_this":"0.5","ep_next":"0.5","special":false,"minutes":585,"goals_scored":0,"assists":0,"clean_sheets":1,"goals_conceded":9,"own_goals":0,"penalties_saved":0,"penalties_missed":0,"yellow_cards":0,"red_cards":0,"saves":27,"bonus":3,"bps":130,"influence":"205.0","creativity":"0.0","threat":"0.0","ict_index":"20.4","ea_index":0,"element_type":1,"team":1},{"id":2,"photo":"80201.jpg","web_name":"Leno","team_code":3,"status":"a","code":80201,"first_name":"Bernd","second_name":"Leno","squad_number":19,"news":"","now_cost":49,"news_added":null,"chance_of_playing_this_round":null,"chance_of_playing_next_round":null,"value_form":"0.5","value_season":"21.6","cost_change_start":-1,"cost_change_event":0,"cost_change_start_fall":1,"cost_change_event_fall":0,"in_dreamteam":false,"dreamteam_count":1,"selected_by_percent":"4.0","form":"2.6","transfers_out":250834,"transfers_in":339095,"transfers_out_event":0,"transfers_in_event":0,"loans_in":0,"loans_out":0,"loaned_in":0,"loaned_out":0,"total_points":106,"event_points":3,"points_per_game":"3.3","ep_this":"3.1","ep_next":"3.7","special":false,"minutes":2835,"goals_scored":0,"assists":0,"clean_sheets":6,"goals_conceded":42,"own_goals":0,"penalties_saved":0,"penalties_missed":0,"yellow_cards":0,"red_cards":0,"saves":105,"bonus":5,"bps":568,"influence":"807.2","creativity":"0.0","threat":"0.0","ict_index":"80.5","ea_index":0,"element_type":1,"team":1},{"id":3,"photo":"51507.jpg","web_name":"Koscielny","team_code":3,"status":"a","code":51507,"first_name":"Laurent","second_name":"Koscielny","squad_number":6,"news":"","now_cost":54,"news_added":"2019-05-03T08:31:19Z","chance_of_playing_this_round":100,"chance_of_playing_next_round":100,"value_form":"0.1","value_season":"11.5","cost_change_start":-1,"cost_change_event":0,"cost_change_start_fall":1,"cost_change_event_fall":0,"in_dreamteam":false,"dreamteam_count":1,"selected_by_percent":"0.9","form":"0.6","transfers_out":92187,"transfers_in":128478,"transfers_out_event":0,"transfers_in_event":0,"loans_in":0,"loans_out":0,"loaned_in":0,"loaned_out":0,"total_points":62,"event_points":1,"points_per_game":"3.6","ep_this":"1.1","ep_next":"2.7","special":false,"minutes":1329,"goals_scored":3,"assists":0,"clean_sheets":3,"goals_conceded":23,"own_goals":0,"penalties_saved":0,"penalties_missed":0,"yellow_cards":1,"red_cards":0,"saves":0,"bonus":8,"bps":319,"influence":"456.4","creativity":"29.5","threat":"105.0","ict_index":"59.1","ea_index":0,"element_type":2,"team":1},

这是我正在使用的Python脚本

import csv
import json

json_data = open ("fpl.json")
data = json.load(json_data)

f = csv.writer(open("fpl.csv","wb+"))

arr = {}

for i in data:
    pp = data [i] ["player_points"]
    array = pp["all"]
    for j in array:

        try:
            j.insert(0,str(data[i]["first_name"]))
        except:
            j.insert(0,'error')

        try:
            j.insert(0,str(data[i]["second_name"]))
        except:
            j.insert(0,'error')

        try:
            j.insert(0,str(data[i]["total_points"]))
        except:
            j.insert(0,'error')

        try:
            f.writerow(j)
        except:
            f.writerow(['error','error'])

json_data.close()

我期望CSV中有以下输出

first_name     second_name    total_points     event_points
Petr            Cech           24               0

Tags: nameineventjsondatacodeoutchange