0

I am writing a script to create a csv based on a parsed JSON. I am able to read the JSON, but get hung up with a TypeError: list indices must be integers, not dict.

I have a previous version that is working, but with a different JSON, so the structure is slightly different, and I am working on my reverse engineering skills, but am stumped on extracting with the slight differences.

JSON is structured thus:

{
 "league": {
   "games": {
     "0": [
      {
       "game": {
          "game_number": "game_1",
          "season": "2019",
          "start_time": "Sat, 13 Apr 2019 23:00:00", 
          "team_id": [
              {
              "away_team": "team_x"
              },
              {
              "home_team": "team_a"
              },
           ],
          },
         },
        ],
       },
      },
}
data = json_parsed['league']['games'][0]

with open('./soccer_041519.csv', 'w+') as csvFile:

    for game in data:
        gameid = data[game]['game_number']
        start_time = data[game]['start_time']
        home_team_id = data[game]['home_team']
        away_team_id = data[game]['away_team']
csvFile.write("%s @ %s,%s, ,%s\n"%(away_team_id, home_team_id, gameid, start_time))     


The values should be written to the CSV
1
  • 1
    That's not valid JSON. "game_number": "game_1" needs a trailing comma, as do the next two items. Also, the dictionaries in team_id need to be separated with commas. Commented Apr 15, 2019 at 15:01

2 Answers 2

1

a few things I've noticed with the json data:

  1. Missing tons of commas

  2. You're trying to call an element by it's index value 0, when it's a key of "0"

So fixing 1:

{
 "league": {
   "games": {
     "0": [
      {
       "game": {
          "game_number": "game_1"   <---- need comma
          "season": "2019"    <-----need comma
          "start_time": "Sat, 13 Apr 2019 23:00:00"  <-----need comma
          "team_id": [
              {
              "away_team": "team_x"
              }   <-----need comma
              {
              "home_team": "team_a"
              }
           ]
          }
         }
        ]
       }
      }
}

So fix that:

json_parsed = {"league": {
   "games": {
     "0": [
      {
       "game": {
          "game_number": "game_1",
          "season": "2019",
          "start_time": "Sat, 13 Apr 2019 23:00:00" ,
          "team_id": [
              {
              "away_team": "team_x"
              },
              {
              "home_team": "team_a"
              }
           ]
          }
         }
        ]
       }
      }
}

Fixing 2:

data = json_parsed['league']['games']['0']

Then for your loop:

for game in data:
    gameid = game['game_number']
    start_time = game['start_time']
    home_team_id = game['home_team_id']
    away_team_id = game['away_team_id']         
Sign up to request clarification or add additional context in comments.

1 Comment

That give the following error:Traceback (most recent call last): File "soccer_csv_create.py", line 25, in <module> gameid = data[game]['game_number'] TypeError: list indices must be integers, not dict
0
for game in data:
    gameid = data[game]['game_number']

You don't need to refer to data, as your loop already gets each specific game.

Use gameid = game['game']['game_number'] instead.

Also, your use of the team_id list is confusing. Why is it a list? Usually a list is used when you don't know how many items there will be, but in this case it seems like you do know that -- there will always be one home team and one away team, right?

It seems like a single dictionary would be a better way to represent team_id:

"team_id": {
    "away_team": "team_x"
    "home_team": "team_a"
}

Or, even better, why does this sub-dictionary exist at all? It seems like away_team_id and home_team_id could be elements in the main-level dictionary. Why do they need to be segregated away on their own?

3 Comments

I don't know why the JSON is that way, but it is. Now the error has changed: Traceback (most recent call last): File "soccer_csv_create.py", line 27, in <module> home_team_id = game['game']['team_ids']['home_team_id'] TypeError: list indices must be integers, not str
That's because game['game']['team_ids'] is a list, but you're trying to treat it like a dictionary.
This not a JSON that I created, this i s just what the API spits out. I am able to get some of the information out now, but it doesn't seem to be looping through. It is only returning info for one game, and there are many. home_team_id = game['game']['team_ids'][1]['home_team_id'] away_team_id = game['game']['team_ids'][0]['away_team_id']

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.