L
L
Lavrenty Fedotov2017-03-30 21:31:53
Python
Lavrenty Fedotov, 2017-03-30 21:31:53

list indices must be integers error. How to fix?

Through the request parse JSON from the site

"result":{"stats":[{"hi":"0.00","bye":"0.00","algo":9"},{"hi":"0.00","bye":"0.00","algo":8"},{"hi":"0.00","bye":"0.00","algo":7"}]}

In JSON, the elements differ only by the algo
Parser:
r2 = requests.get('https://www..com/api?) 
      data = r2.json()
      for i in data['result']['stats']:
      x1 = data['result']['stats'][i]['hi']
      z1 = data['result']['stats'][i]['bye']
      bot.send_message(cid, str(x1) + str(z1))

I want to send a message for every {} in result|stats.
Gives an error "list indices must be integers, not dict"
I understand that this is because of [] in json, but I don't know how to fix
it

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2017-03-30
@Survtur

First, split the lines of the form Into several lines:

_x = data['result']
_x = _x['stats']
_x = _x[i]
x1 = _x['hi']

That way you can find exactly where the error is.
Then try to see what the values ​​are when running this timeline by adding a print() pair in front of it.
In general, yes ... answers. mail. ru, some ...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question