0

I'm getting the data from a json file in a website. So I did this.

import requests

r = requests.get('https://corona.lmao.ninja/historical/spain')
r = r.json()

for data in r['timeline']['cases']:
    print(data)

But I'm not able to take the number cases, just the date. I tried with data[0] but it just take the first char from the first element. So I don't have any idea how I could take the second element.

enter image description here

Thanks and sorry for my English.

  • `r['timeline']['cases']` is a dict. To iterate over the key, value pairs of a dict, use `.items` like so: `for date, value in r['timeline']['cases'].items(): ...` – juanpa.arrivillaga Mar 24 '20 at 04:51
  • Sorry for bother you once again, what if I want to get the date/value as array? It's necessary to iterate the data and enter each value in a new array, or is there an easier way? – Georgia Fernández Mar 24 '20 at 04:57
  • Just `dates = list(r['timeline']['cases'])` if you want the dates in a list, or `values = list(r['timeline']['cases'].values())` if you want the values, or for the key-value pairs, i.e. `items = [(k1, v1), (k2, v2), ..., (kn, vn)]` then just `items = list(r['timeline']['cases'].items())` all of which do iterate over the data underneath the hood, of course. – juanpa.arrivillaga Mar 24 '20 at 05:00

0 Answers0