Python handle API [closed]

-1

I want to use the following API response: link

Then I want to divide it into blocks like this: [1456617600,434.3867,434.3867,433,781,433,781.4.15450000] and put it on a list to work with the data.

I was doing so but it is not working:

 r=requests.get('https://cex.io/api/ohlcv/hd/20160228/BTC/USD')
    r= json.loads(r.text)
    list= []
    for row in r:
        list.append(row)
    
asked by anonymous 29.06.2017 / 20:37

1 answer

0

When you use

r = json.loads(r.text)

json will transform the text that came from request into a dictionary with two keys only, time and data1m .

The blocks you're talking about are under the data1m key, so you'll have to get them there. The only problem is what to do

r["data1m"]

returns a string (you can check with type(r["data1m"]) )

So you'll have to turn that string into a list that you can work with too. You can do this if you use json.loads new:

r = requests.get('https://cex.io/api/ohlcv/hd/20160228/BTC/USD')
r = json.loads(r.text)
lista = json.loads(r["data1m"])

At this point, lista is already a list whose entries are the blocks you are talking about. For example,

lista[0]

returns

[1456617600, 434.3867, 434.3867, 433.781, 433.781, 4.1545]
    
30.06.2017 / 00:07