Feb-13-2025, 07:24 PM
Hi guys,
i have two json files
code works fine for test1.json, but it gives "TypeError: string indices must be integers" error for test2.json
can you help me to fix it please?
thank you.
i have two json files
code works fine for test1.json, but it gives "TypeError: string indices must be integers" error for test2.json
can you help me to fix it please?
thank you.
import json import datetime import contextlib import pandas as pd class Game: @classmethod def from_json(cls, json_record): game = cls() game.country = json_record["CN"] game.league = json_record["L"] game.date = datetime.datetime.fromtimestamp(json_record["S"]) game.hometeam = None game.awayteam = None game.odds1 = None game.oddsX = None game.odds2 = None game.over = None game.gline = None game.under = None if game_a := json_record: with contextlib.suppress(IndexError): game.hometeam = game_a.get("O1") with contextlib.suppress(IndexError): game.awayteam = game_a.get("O2") if game_e := json_record.get("E"): with contextlib.suppress(IndexError): game.odds1 = game_e[0].get("C") with contextlib.suppress(IndexError): game.oddsX = game_e[1].get("C") with contextlib.suppress(IndexError): game.odds2 = game_e[2].get("C") if game_f := json_record.get("AE"): with contextlib.suppress(IndexError): game.over = game_f[1]["ME"][4].get("C") with contextlib.suppress(IndexError): game.gline = game_f[1]["ME"][4].get("P") with contextlib.suppress(IndexError): game.under = game_f[1]["ME"][5].get("C") return game def __str__(self): return ','.join( str(item) for item in ( self.country, self.league, self.date, self.hometeam, self.awayteam, self.odds1, self.oddsX, self.odds2, self.over, self.gline, self.under)) with open("test2.json", encoding="UTF-8") as f: jsondata = json.load(f) games = [Game.from_json(game) for game in jsondata["Value"] if "KLASK" not in game["L"] and "5x5" not in game["L"] and "Home (Goals)" not in game["O1"] and "(Statistics)" not in game["O1"] and "(3x3)" not in game["O1"]] df=pd.DataFrame(games) df.to_excel("test.xlsx") print("Saved.")
Error:Traceback (most recent call last):
File "c:\Json2Excel\Convert_Json_to_Excel.py", line 52, in <module>
games = [Game.from_json(game) for game in jsondata["Value"] if "KLASK" not in game["L"] and "5x5" not in game["L"] and "Home (Goals)" not in game["O1"] and "(Statistics)" not in game["O1"] and "(3x3)" not in game["O1"]]
File "c:\Json2Excel\Convert_Json_to_Excel.py", line 52, in <listcomp>
games = [Game.from_json(game) for game in jsondata["Value"] if "KLASK" not in game["L"] and "5x5" not in game["L"] and "Home (Goals)" not in game["O1"] and "(Statistics)" not in game["O1"] and "(3x3)" not in game["O1"]]
TypeError: string indices must be integers
Attached Files