How to add values of a request:

Asked

Viewed 98 times

0

Example:

request = requests.get('https://api.amountofsomething.com')
quantidade = requisição.json()['amount']

This request returns the quantity, for example: {[{25}{120}{158}{0}{2}{10}]}

I need to add the values without patterns. I tried this way:

amount0 = amount[0]['amount']

amount1 = amount[1]['amount']

amount2 = amount[2]['amount']

sum0 = amount0+amount02
...

For large amounts of data, the code will become too much. I tried these ways:

sum0 =  amount [0+2]['amount']

sum1 = amount [[1] + [18]]['amount']

sum2 = amount [0]['amount'] + amount [1]['amount']

None of these options worked. Can anyone help me?

  • I did not notice this way of adding or this data format of quantity, so I will not put answer but make a suggestion based on what I realized (I think). Based on your first attempt, try sum(amount['amount'] for amount in request.json()), based on your attempt I believe you can do this https://ideone.com/dbKFMu

  • Also I could not understand what you need. Can you [Edit] the question by placing the full return of the request and describe what the desired result?

  • Put the return value of the request as it comes - what is in your example is not valid JSON, nor Python - and can not do anything, since it is not correct. Programming is neither magic nor kick. [{"amount": 30}, {"amount":50}] is completely different from [{30, 50}] or of [{30},{50}].

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.