What would be the point in having a part of the program when you will not be able to continue working on it? I was trying to help you understand how it works.
Also I know nothing about the format of the input data or how are you planning to continue processing the data so I can only help you with particular problems and explain you the concepts.
How to fix the problem in your code you were complaining about:
Take your code from here:
Replace this:
if game_a := json_record.get:
with contextlib.suppress(IndexError):
game.hometeam = game_a.get("O1")
with contextlib.suppress(IndexError):
game.awayteam = game_a.get("O2")
I know nothing about the data so I can only suppose that your original code for extracting the first record was OK.
If the key "O1" can be missing (you will get KeyError) in the input data and you want to tolerate the missing values, use json_record.get("O1") instead of json_record["O1"]. Of course the same holds for the key "O2".
import json
import contextlib
class Game:
@classmethod
def from_json(cls, json_record):
game = cls()
game.country = json_record["CN"]
game.league = json_record["L"]
game.date = json_record["S"]
game.odds1 = None
game.oddsX = None
game.odds2 = None
game.over = None
game.gline = None
game.under = None
game.hometeam = json_record("O1")
game.awayteam = json_record("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")
with contextlib.suppress(IndexError):
game.over = game_e[8].get("C")
with contextlib.suppress(IndexError):
game.gline = game_e[8].get("P")
with contextlib.suppress(IndexError):
game.under = game_e[9].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("18july.json", encoding="UTF-8") as f:
jsondata = json.load(f)
games = [Game.from_json(game) for game in jsondata["Value"]]
for game in games:
print(game)
this gave this error output
Traceback (most recent call last):
File "c:\Users\monst\Downloads\python_files\vaclav3.py", line 45, in <module>
games = [Game.from_json(game) for game in jsondata["Value"]]
File "c:\Users\monst\Downloads\python_files\vaclav3.py", line 45, in <listcomp>
games = [Game.from_json(game) for game in jsondata["Value"]]
File "c:\Users\monst\Downloads\python_files\vaclav3.py", line 18, in from_json
game.hometeam = json_record("O1")
TypeError: 'dict' object is not callable
import json
import contextlib
class Game:
@classmethod
def from_json(cls, json_record):
game = cls()
game.country = json_record["CN"]
game.league = json_record["L"]
game.date = json_record["S"]
game.hometeam = json_record["O1"]
game.awayteam = json_record["O2"]
game.odds1 = None
game.oddsX = None
game.odds2 = None
game.over = None
game.gline = None
game.under = None
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")
with contextlib.suppress(IndexError):
game.over = game_e[8].get("C")
with contextlib.suppress(IndexError):
game.gline = game_e[8].get("P")
with contextlib.suppress(IndexError):
game.under = game_e[9].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("18july.json", encoding="UTF-8") as f:
jsondata = json.load(f)
games = [Game.from_json(game) for game in jsondata["Value"]]
for game in games:
print(game)
this code gave error output too
Traceback (most recent call last):
File "c:\Users\monst\Downloads\python_files\vaclav3.py", line 44, in <module>
games = [Game.from_json(game) for game in jsondata["Value"]]
File "c:\Users\monst\Downloads\python_files\vaclav3.py", line 44, in <listcomp>
games = [Game.from_json(game) for game in jsondata["Value"]]
File "c:\Users\monst\Downloads\python_files\vaclav3.py", line 12, in from_json
game.awayteam = json_record["O2"]
KeyError: 'O2'
import json
import contextlib
class Game:
@classmethod
def from_json(cls, json_record):
game = cls()
game.country = json_record["CN"]
game.league = json_record["L"]
game.date = json_record["S"]
game.odds1 = None
game.oddsX = None
game.odds2 = None
game.over = None
game.gline = None
game.under = None
game.hometeam = json_record.get["O1"]
game.awayteam = json_record.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")
with contextlib.suppress(IndexError):
game.over = game_e[8].get("C")
with contextlib.suppress(IndexError):
game.gline = game_e[8].get("P")
with contextlib.suppress(IndexError):
game.under = game_e[9].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("18july.json", encoding="UTF-8") as f:
jsondata = json.load(f)
games = [Game.from_json(game) for game in jsondata["Value"]]
for game in games:
print(game)
it gives this error
Traceback (most recent call last):
File "c:\Users\monst\Downloads\python_files\vaclav3.py", line 45, in <module>
games = [Game.from_json(game) for game in jsondata["Value"]]
File "c:\Users\monst\Downloads\python_files\vaclav3.py", line 45, in <listcomp>
games = [Game.from_json(game) for game in jsondata["Value"]]
File "c:\Users\monst\Downloads\python_files\vaclav3.py", line 18, in from_json
game.hometeam = json_record.get["O1"]
TypeError: 'builtin_function_or_method' object is not subscriptable
import json
import contextlib
class Game:
@classmethod
def from_json(cls, json_record):
game = cls()
game.country = json_record["CN"]
game.league = json_record["L"]
game.date = json_record["S"]
game.hometeam = json_record.get["O1"]
game.awayteam = json_record.get["O2"]
game.odds1 = None
game.oddsX = None
game.odds2 = None
game.over = None
game.gline = None
game.under = None
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")
with contextlib.suppress(IndexError):
game.over = game_e[8].get("C")
with contextlib.suppress(IndexError):
game.gline = game_e[8].get("P")
with contextlib.suppress(IndexError):
game.under = game_e[9].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("18july.json", encoding="UTF-8") as f:
jsondata = json.load(f)
games = [Game.from_json(game) for game in jsondata["Value"]]
for game in games:
print(game)
and this version gives this error
Traceback (most recent call last):
File "c:\Users\monst\Downloads\python_files\vaclav3.py", line 44, in <module>
games = [Game.from_json(game) for game in jsondata["Value"]]
File "c:\Users\monst\Downloads\python_files\vaclav3.py", line 44, in <listcomp>
games = [Game.from_json(game) for game in jsondata["Value"]]
File "c:\Users\monst\Downloads\python_files\vaclav3.py", line 11, in from_json
game.hometeam = json_record.get["O1"]
TypeError: 'builtin_function_or_method' object is not subscriptable
import json
import contextlib
class Game:
@classmethod
def from_json(cls, json_record):
game = cls()
game.country = json_record["CN"]
game.league = json_record["L"]
game.date = json_record["S"]
game.odds1 = None
game.oddsX = None
game.odds2 = None
game.over = None
game.gline = None
game.under = None
if game := json_record:
with contextlib.suppress(IndexError):
game.hometeam = json_record.get["O1"]
with contextlib.suppress(IndexError):
game.awayteam = json_record.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")
with contextlib.suppress(IndexError):
game.over = game_e[8].get("C")
with contextlib.suppress(IndexError):
game.gline = game_e[8].get("P")
with contextlib.suppress(IndexError):
game.under = game_e[9].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("18july.json", encoding="UTF-8") as f:
jsondata = json.load(f)
games = [Game.from_json(game) for game in jsondata["Value"]]
for game in games:
print(game)
it also gives error
Traceback (most recent call last):
File "c:\Users\monst\Downloads\python_files\vaclav3.py", line 48, in <module>
games = [Game.from_json(game) for game in jsondata["Value"]]
File "c:\Users\monst\Downloads\python_files\vaclav3.py", line 48, in <listcomp>
games = [Game.from_json(game) for game in jsondata["Value"]]
File "c:\Users\monst\Downloads\python_files\vaclav3.py", line 20, in from_json
game.hometeam = json_record.get["O1"]
TypeError: 'builtin_function_or_method' object is not subscriptable
import json
import contextlib
class Game:
@classmethod
def from_json(cls, json_record):
game = cls()
game.country = json_record["CN"]
game.league = json_record["L"]
game.date = json_record["S"]
game.odds1 = None
game.oddsX = None
game.odds2 = None
game.over = None
game.gline = None
game.under = None
if game := json_record:
with contextlib.suppress(IndexError):
game.hometeam = game.get["O1"]
with contextlib.suppress(IndexError):
game.awayteam = game.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")
with contextlib.suppress(IndexError):
game.over = game_e[8].get("C")
with contextlib.suppress(IndexError):
game.gline = game_e[8].get("P")
with contextlib.suppress(IndexError):
game.under = game_e[9].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("18july.json", encoding="UTF-8") as f:
jsondata = json.load(f)
games = [Game.from_json(game) for game in jsondata["Value"]]
for game in games:
print(game)
error
Traceback (most recent call last):
File "c:\Users\monst\Downloads\python_files\vaclav3.py", line 48, in <module>
games = [Game.from_json(game) for game in jsondata["Value"]]
File "c:\Users\monst\Downloads\python_files\vaclav3.py", line 48, in <listcomp>
games = [Game.from_json(game) for game in jsondata["Value"]]
File "c:\Users\monst\Downloads\python_files\vaclav3.py", line 20, in from_json
game.hometeam = game.get["O1"]
TypeError: 'builtin_function_or_method' object is not subscriptable
You cannot freely replace round brackets () with square brackets []. They have very different meaning. In programming, this very often applies to all characters!
In this context the meaning is:
() - function call operator - You use it to call a function or a method. Here get is a method of dict (dictionary) objects. In this statement we need to call it.
[] - index / subscript operator - You use it to access dictionary or list items. The method get does not provide you a way to be subscripted. You receive the error TypeError: ⌠object is not subscriptable when you attempt to do this impossible operation.
If you are not sure how to change the faulty statement go back to my previous post to see the correct shape of it.
I understand well that everything is more complicated in difficult life situations. I hope it will be better for you soon.
Please understand that here we want to help you to improve as an independent user of Python and programmer, not to write the programs for you. This process needs some patience and determination. Feel free to ask for further help.