首页 > 解决方案 > Flask TypeError:字符串索引必须是整数

问题描述

我的 Flask 项目中的 delete 方法出现此错误。 TypeError:字符串索引必须是整数

我该如何解决?

def delete(self,player_name):
        global nba_players_json
        nba_players_json = list(filter(lambda x : x['name'] != player_name, nba_players_json))
        return {'message' : 'Item deleted'}

这是我的字典的样子:

"AJ Price": {
        "Name": "AJ Price",
        "Games Played": "26",
        "MIN": "324",
        "PTS": "133",
        "FGM": "51",
        "FGA": "137",
        "FG%": "37.2",
        "3PM": "15",
        "3PA": "57",
        "3P%": "26.3",
        "FTM": "16",
        "FTA": "24",
        "FT%": "66.7",
        "OREB": "6",
        "DREB": "26",
        "REB": "32",
        "AST": "46",
        "STL": "7",
        "BLK": "0",
        "TOV": "14",
        "PF": "15",
        "EFF": "110",
        "AST/TOV": "3.29",
        "STL/TOV": "0.5",
        "Age": "29",
        "Birth_Place": "us",
        "Birthdate": "October 7, 1986",
        "Collage": "University of Connecticut",
        "Experience": "5",
        "Height": "185",
        "Pos": "PG",
        "Team": "PHO",
        "Weight": "81.45",
        "BMI": "23.79839299"}

标签: python

解决方案


见下文

nba_players_json = {"AJ Price": {
        "Name": "AJ Price",
        "Games Played": "26",
        "MIN": "324",
        "PTS": "133",
        "FGM": "51",
        "FGA": "137",
        "FG%": "37.2",
        "3PM": "15",
        "3PA": "57",
        "3P%": "26.3",
        "FTM": "16",
        "FTA": "24",
        "FT%": "66.7",
        "OREB": "6",
        "DREB": "26",
        "REB": "32",
        "AST": "46",
        "STL": "7",
        "BLK": "0",
        "TOV": "14",
        "PF": "15",
        "EFF": "110",
        "AST/TOV": "3.29",
        "STL/TOV": "0.5",
        "Age": "29",
        "Birth_Place": "us",
        "Birthdate": "October 7, 1986",
        "Collage": "University of Connecticut",
        "Experience": "5",
        "Height": "185",
        "Pos": "PG",
        "Team": "PHO",
        "Weight": "81.45",
        "BMI": "23.79839299"}}

def delete(player_name):
        global nba_players_json
        if player_name in nba_players_json:
          del nba_players_json[player_name]
          return {'message' : 'Item deleted'}
        else:
          return {'message' : 'Item not deleted'}

print(delete('AJ Price'))

推荐阅读