0

Learning python for the first time and it's I'm reading the JSON and decoding it correctly. I can loop through the names with the first for loop so I figured I was getting the value of names correctly. When I try to set that value to a variable I get the following error.

Traceback (most recent call last):
  File "./logger.py", line 70, in <module>
    winnerName = jsonData['players'][winner].get('name')
TypeError: list indices must be integers, not unicode

Python code:

jsonData = json.load(open('file.json'))

## Functions correctly and prints each name out.
for i in range(0,7):
  print jsonData['players'][i].get('name')

gameMatches = jsonData['games'][game]['matches']
for match in gameMatches:
  winnerBool = 1
  winner = -1
  loser = -1
  winnerName = ""
  loserName = ""
  matchCounter = 1

  ## Set the winner first then check to see who lost
  for key, value in sorted(match.iteritems(), reverse=True):
    if winnerBool:
      winner = value
  ## This is where the error keeps pointing
      winnerName = jsonData['players'][winner].get('name')
      winnerBool = 0
    elif winner != value:
      loserName = jsonData['players'][loser].get('name')
      loser = value

  print winnerName
  print loserName

JSON file:

{
   "players":[
      {"name":"Donatello","img":"\/img\/players\/1.jpg","rol":"\/img\/players\/r1.jpg"},
      {"name":"Leonardo","img":"\/img\/players\/2.jpg","rol":"\/img\/players\/r2.jpg"},
      {"name":"Michelangelo","img":"\/img\/players\/3.jpg","rol":"\/img\/players\/r3.jpg"},
      {"name":"Raphael","img":"img\/players\/4.jpg","rol":"img\/players\/r4.jpg"},
      {"name":"Shredder","img":"\/img\/players\/5.jpg","rol":"\/img\/players\/r5.jpg"},
      {"name":"Rocksteady","img":"\/img\/players\/6.jpg","rol":"\/img\/players\/r6.jpg"},
      {"name":"Bebop","img":"\/img\/players\/7.jpg","rol":"\/img\/players\/r7.jpg"},
      {"name":"Foot Soldier","img":"\/img\/players\/8.jpg","rol":"\/img\/players\/r8.jpg"}
      ],
   "games":[
      {"matches":[
         {"player1":"0","player2":"2","winner":"0"},
         {"player1":"1","player2":"3","winner":"3"},
         {"player1":"4","player2":"5","winner":"4"},
         {"player1":"6","player2":"7","winner":"6"},
         {"player1":"0","player2":"3","winner":"0"},
         {"player1":"4","player2":"6","winner":"4"},
         {"player1":"0","player2":"4","winner":"0"}
         ]
      },
      {"matches":[
         {"player1":"0","player2":"2","winner":"2"},
         {"player1":"1","player2":"3","winner":"3"},
         {"player1":"4","player2":"5","winner":"4"},
         {"player1":"6","player2":"7","winner":"7"},
         {"player1":"2","player2":"3","winner":"2"},
         {"player1":"4","player2":"7","winner":"4"},
         {"player1":"2","player2":"4","winner":"4"}
         ]
      }
   ]
}
4
  • Could you post the full traceback? And the full JSON file? Because what you posted has no 'games' section, but your code refers to jsonData['games']. Commented Mar 1, 2012 at 20:57
  • Without checking whether you actually access the correct data, your JSON does not contain any numbers, only strings. So winner contains a string. You have to convert it to a number first. Commented Mar 1, 2012 at 20:58
  • "Foot Clan"? Don't you mean "Foot Soldier"? Commented Mar 1, 2012 at 20:59
  • Sorry about cropping it, didn't know how much to include. I've added the fulltraceback and JSON. Thanks Ignacio lol didn't notice I typed that instead of singular. Commented Mar 1, 2012 at 21:09

1 Answer 1

1

Use

winner = int(value)

To convert from "1" (string) to 1 (integer)

EDITED to clarify:

when you do this....

for key, value in sorted(match.iteritems(), reverse=True):
    #...

...your key and value variables will be strings. I am assuming you are iterating over this part of your JSON:

{"matches":[
     {"player1":"0","player2":"2","winner":"0"},
     {"player1":"1","player2":"3","winner":"3"},

Therefore, when you assign...

winner = value

...and try to look up...

winnerName = jsonData['players'][winner].get('name')

...you get a...

TypeError: list indices must be integers, not unicode

...because you are saying:

x = list["4"]

where it should say:

x = list[4]
Sign up to request clarification or add additional context in comments.

2 Comments

Sorry I didn't give enough information about the problem the question has been edited to clarify what is the issue. I'm trying to get the name value in the players array and set it to a variable.
Oh wow yeah that makes sense now. You did answer it but I was thinking the issue was with storing the name not the winner/loser "int" being a string. Thank you.

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.