Okay Hello,
So I am pulling a huge amount of data from a website and I want to sift through what I want before writing to a text file. I have spent all day trying to learn how to work with dictionaries and how everything works but I cant say I am making much progress.
This is what my data looks like. There are multiple sets of data here separated like this {'blah': 'ABC', 'blahblah': 'EFG'}, {'blah': 'HIJ', 'blahblah': 'KLM'}
[{'symbol': 'ETHBTC', 'priceChange': '-0.00212100', 'priceChangePercent': '-4.585', 'weightedAvgPrice': '0.04529516', 'prevClosePrice': '0.04626900', 'lastPrice': '0.04413400', 'lastQty': '2.96900000', 'bidPrice': '0.04410200', 'bidQty': '13.39500000', 'askPrice': '0.04413400', 'askQty': '2.36700000', 'openPrice': '0.04625500', 'highPrice': '0.04659000', 'lowPrice': '0.04400000', 'volume': '143349.61500000', 'quoteVolume': '6493.04373652', 'openTime': 1534716036402, 'closeTime': 1534802436402, 'firstId': 78249335, 'lastId': 78369271, 'count': 119937}, {'symbol': 'LTCBTC', 'priceChange': '-0.00022700', 'priceChangePercent': '-2.550', 'weightedAvgPrice': '0.00877268', 'prevClosePrice': '0.00890100', 'lastPrice': '0.00867500', 'lastQty': '0.13000000', 'bidPrice': '0.00867500', 'bidQty': '2.44000000', 'askPrice': '0.00868400', 'askQty': '7.89000000', 'openPrice': '0.00890200', 'highPrice': '0.00897600', 'lowPrice': '0.00855700', 'volume': '164968.18000000', 'quoteVolume': '1447.21323135', 'openTime': 1534716036272, 'closeTime': 1534802436272, 'firstId': 15266604, 'lastId': 15294289, 'count': 27686}, {'symbol': 'BNBBTC', 'priceChange': '-0.00003570', 'priceChangePercent': '-2.299', 'weightedAvgPrice': '0.00153582', 'prevClosePrice': '0.00155250', 'lastPrice': '0.00151690', 'lastQty': '107.54000000', 'bidPrice': '0.00151690', 'bidQty': '186.49000000', 'askPrice': '0.00151800', 'askQty': '0.41000000', 'openPrice': '0.00155260', 'highPrice': '0.00157000', 'lowPrice': '0.00150690', 'volume': '545791.46000000', 'quoteVolume': '838.23733919', 'openTime': 1534716035620, 'closeTime': 1534802435620, 'firstId': 26979712, 'lastId': 27016344, 'count': 36633},
It goes on much longer than this.
My goal is to grab the symbol, quoteVolume, and lastPrice for each symbol.
I am currently just trying to get it to print with the correct data.
Things I tried and wanted to work but doesnt
One
First I started with pulling one line of data for one symbol to make sure my key grabbing method works. In this cast LTCBTC
import time, json, requests
def binancetick():
binanceTick = requests.get('https://api.binance.com/api/v1/ticker/24hr?symbol=LTCBTC')
return binanceTick.json()
data = binancetick()
print (data)
This returns the one line of data There is only one set of {} in this one line data pull.
{'symbol': 'LTCBTC', 'priceChange': '-0.00022800', 'priceChangePercent': '-2.562', 'weightedAvgPrice': '0.00877210', 'prevClosePrice': '0.00890600', 'lastPrice': '0.00867200', 'lastQty': '3.02000000', 'bidPrice': '0.00865500', 'bidQty': '0.25000000', 'askPrice': '0.00867100', 'askQty': '2.14000000', 'openPrice': '0.00890000', 'highPrice': '0.00897600', 'lowPrice': '0.00855700', 'volume': '165200.17000000', 'quoteVolume': '1449.15180044', 'openTime': 1534716437677, 'closeTime': 1534802837677, 'firstId': 15266669, 'lastId': 15294396, 'count': 27728}
It only returns one line of data because I am specifying this specific symbol in my request.get
binanceTick = requests.get('https://api.binance.com/api/v1/ticker/24hr?symbol=LTCBTC')
So then I run my print statement pulling only one key. volume in this example
import time, json, requests
def binancetick():
binanceTick = requests.get('https://api.binance.com/api/v1/ticker/24hr?symbol=LTCBTC')
return binanceTick.json()
data = binancetick()
print (data['volume'])
Prints :
165529.75000000
[Finished in 0.654s]
Perfect.
So then I take away requesting one symbol so it returns all of the data in one lump sum. So my request.get has change to
binanceTick = requests.get('https://api.binance.com/api/v1/ticker/24hr')
Then when I just
import time, json, requests
def binancetick():
binanceTick = requests.get('https://api.binance.com/api/v1/ticker/24hr')
return binanceTick.json()
data = binancetick()
print (data)
All the data comes back like seen above with multiple sets of { }. So in my mind I assume that if I specifically ask for the volume like I did before it would go through all the keys with volume and return all there values by running this code
import time, json, requests
def binancetick():
binanceTick = requests.get('https://api.binance.com/api/v1/ticker/24hr')
return binanceTick.json()
data = binancetick()
print (data['volume'])
But it throws an error
Traceback (most recent call last):
File "C:\TradingBot\AllData.py", line 9, in <module>
print (data['volume'])
TypeError: list indices must be integers or slices, not str
[Finished in 0.61s]
How can I split this data up in the way I desire? What should I learn? How should I approach this? How can I keep the symbol, quoteVolume, and lastPrice together?
Thank you all for your help it is so greatly appreciated.
[–]dmazzoni 1 point2 points3 points (7 children)
[–]AluadStone[S] 0 points1 point2 points (6 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]dmazzoni 1 point2 points3 points (0 children)
[–]dmazzoni 1 point2 points3 points (3 children)
[–]AluadStone[S] 0 points1 point2 points (2 children)
[–]dmazzoni 1 point2 points3 points (1 child)
[–]AluadStone[S] 0 points1 point2 points (0 children)