从数组dict值设置python变量?

时间:2012-03-01 20:53:27

标签: python json

第一次学习python,我正在阅读JSON并正确解码它。我可以使用第一个for循环遍历名称,所以我认为我正确地获得了名称的值。当我尝试将该值设置为变量时,我得到以下错误。

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代码:

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文件:

{
   "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"}
         ]
      }
   ]
}

1 个答案:

答案 0 :(得分:1)

使用

winner = int(value)

要从"1"(字符串)转换为1(整数)

已编辑澄清:

当你这样做....

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

...您的键和值变量将是字符串。我假设你正在迭代这部分JSON:

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

因此,当您指定...

winner = value

...并试着抬头......

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

...你得到了......

TypeError: list indices must be integers, not unicode

...因为你说:

x = list["4"]

应该说:

x = list[4]