-1

I really have no idea what's going on here -- let me outline the code.

I'm pulling data from the SFG WorldCup API.

Sample output from the API from earlier today:

[
    {
        "match_number": 26,
        "location": "Arena da Baixada",
        "datetime": "2014-06-20T19:00:00.000-03:00",
        "status": "in progress",
        "home_team": {
            "country": "Honduras",
            "code": "HON",
            "goals": 1
        },
        "away_team": {
            "country": "Ecuador",
            "code": "ECU",
            "goals": 2
        },
        "winner": null,
        "home_team_events": [
            {
                "id": 290,
                "type_of_event": "yellow-card",
                "player": "Bernardez",
                "time": "7"
            },
            {
                "id": 291,
                "type_of_event": "goal",
                "player": "Costly",
                "time": "31"
            },
            {
                "id": 294,
                "type_of_event": "substitution-in halftime",
                "player": "J.C. Garcia",
                "time": "46"
            },
            {
                "id": 297,
                "type_of_event": "substitution-in",
                "player": "M. Martinez",
                "time": "71"
            },
            {
                "id": 301,
                "type_of_event": "substitution-in",
                "player": "M. Chavez",
                "time": "83"
            },
            {
                "id": 293,
                "type_of_event": "yellow-card",
                "player": "Bengtson",
                "time": "453"
            }
        ],
        "away_team_events": [
            {
                "id": 292,
                "type_of_event": "goal",
                "player": "E. Valencia",
                "time": "34"
            },
            {
                "id": 295,
                "type_of_event": "yellow-card",
                "player": "A. Valencia",
                "time": "57"
            },
            {
                "id": 296,
                "type_of_event": "goal",
                "player": "E. Valencia",
                "time": "65"
            },
            {
                "id": 298,
                "type_of_event": "yellow-card",
                "player": "E. Valencia",
                "time": "73"
            },
            {
                "id": 299,
                "type_of_event": "yellow-card",
                "player": "Montero",
                "time": "80"
            },
            {
                "id": 300,
                "type_of_event": "substitution-in",
                "player": "Mendez",
                "time": "82"
            },
            {
                "id": 302,
                "type_of_event": "substitution-in",
                "player": "Gruezo",
                "time": "83"
            }
        ]
    }
]

With this data, my problem is that getting data in the for loop works, but in a while loop below it doesn't.

resp is the response from the api...

     for jogo in resp.json():
         score = jogo['home_team']['goals']
     jogo = resp.json()
     while True:
         newscore = int(jogo['home_team']['goals'])

The actual code is a little different (the jogo['home_team']['goals'] in the while loop is in an if statement, but this is the basic skeleton of it.

The error I get is on the one in the while, and it says TypeError: list indices must be integers, not str

I'm not sure if it's relevant that I'm comparing it to an integer (I have it wrapped in an int()).

What's causing this, and why isn't it being called on the first one.

Thanks!

evamvid

PS let me know if this doesn't make any sense =)

Lev Levitsky
  • 63,701
  • 20
  • 147
  • 175
evamvid
  • 831
  • 6
  • 18
  • 40
  • Print the `jogo` in your loop. See what it is. If `resp.json()` is what you pasted, i don't see a problem.. – aIKid Jun 21 '14 at 03:33
  • 1
    For further help, please post the entire related code, and the complete traceback, thanks! – aIKid Jun 21 '14 at 03:35
  • Has `resp.json()` already been parsed? If not you'll need to parse it (eg with `json.loads(resp.json())`) before you can iterate over the elements (otherwise you're just iterating over characters in a string). – Peter Gibson Jun 21 '14 at 03:38
  • I added the variable declaration to the code. – evamvid Jun 21 '14 at 03:39

1 Answers1

0

With the new code posted it makes more sense:

 # here, jogo takes the value of each dict{} within the outer list[]
 for jogo in resp.json():
     score = jogo['home_team']['goals']

 # while here it is the outer list itself 
 jogo = resp.json()
 while True:
     newscore = int(jogo['home_team']['goals'])

If you know there is only going to be a single result in the list, you can just use

 jogo = resp.json()[0]

The error is telling you that jogo expects an integer index (implying it is a list type or similar), but you're passing it a string index (showing you expected it to be a dict). This was the statement causing the error:

...jogo['home_team']...

The outer int call had no effect.

Peter Gibson
  • 19,086
  • 7
  • 60
  • 64
  • @evamvid No problem, take a look at this question for method on debugging Python code (http://stackoverflow.com/questions/1623039/python-debugging-tips). You could probably have tracked this down yourself in a couple of minutes :) – Peter Gibson Jun 21 '14 at 03:51
  • The API I'm using is very time-specific -- it depends on whether or not a game is being played. I'll accept the answer tomorrow after I can confirm that it works. Thanks for the debugging info!! – evamvid Jun 21 '14 at 03:53