Python Handle API

Asked

Viewed 107 times

-1

I want to use the following API response: https://cex.io/api/ohlcv/hd/20160228/BTC/USD

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 it like this but it’s 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)
  • Mariana, this is the [en.so]. You can click [Edit] and translate your question or, if you prefer, you can post it on [so].

1 answer

0


When you use

r = json.loads(r.text)

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

The blocks you speak of are under the key data1m, so you’re gonna have to pick them up there. The only problem is what to do

r["data1m"]

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

so you’re gonna have to turn that one too string on a list you can work with. You can do this by using json.loads again:

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 of which you speak. For example,

lista[0]

return

[1456617600, 434.3867, 434.3867, 433.781, 433.781, 4.1545]

Browser other questions tagged

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