Python getting only certain variable from a tuple
Question:
I am getting a tuple result from the binance API but I would like to get only price from the tuple into a float variable
What I am getting when I print:
{'symbol': 'BTCUSDT', 'orderId': 16742382780, 'orderListId': -1, 'clientOrderId': 'Y9xSgRpsbgWC9eSBXDMdoI', 'transactTime': 1671964493104, 'price': '0.00000000', 'origQty': '0.03000000', 'executedQty': '0.03000000', 'cummulativeQuoteQty': '504.77939880', 'status': 'FILLED', 'timeInForce': 'GTC', 'type': 'MARKET', 'side': 'BUY', 'workingTime': 1671964493104, 'fills': [{'price': '16825.96000000', 'qty': '0.01004000', 'commission': '0.00000000', 'commissionAsset': 'BNB', 'tradeId': 2379384577}, {'price': '16825.99000000', 'qty': '0.01996000', 'commission': '0.00000000', 'commissionAsset': 'BNB', 'tradeId': 2379384578}], 'selfTradePreventionMode': 'NONE'}
What I want is withdraw float only from;
{'price': '16825.96000000'
as a float
myFloat = 16825.96000000
Mind you there are two ‘price’ one is 0.00000 the other is 16825.96000000
Answers:
What you are getting is not a tuple but a dictionary. Let’s say the dictionary is called my_dic
. Then what you want is this:
my_price = my_dic["fills"][0]["price"]
Because my_dic["fills"]
will give you a list, of which the first element (index 0) is again a dictionary.
Note that this will be a string, to turn it into a number you will want to do float(my_price)
I am getting a tuple result from the binance API but I would like to get only price from the tuple into a float variable
What I am getting when I print:
{'symbol': 'BTCUSDT', 'orderId': 16742382780, 'orderListId': -1, 'clientOrderId': 'Y9xSgRpsbgWC9eSBXDMdoI', 'transactTime': 1671964493104, 'price': '0.00000000', 'origQty': '0.03000000', 'executedQty': '0.03000000', 'cummulativeQuoteQty': '504.77939880', 'status': 'FILLED', 'timeInForce': 'GTC', 'type': 'MARKET', 'side': 'BUY', 'workingTime': 1671964493104, 'fills': [{'price': '16825.96000000', 'qty': '0.01004000', 'commission': '0.00000000', 'commissionAsset': 'BNB', 'tradeId': 2379384577}, {'price': '16825.99000000', 'qty': '0.01996000', 'commission': '0.00000000', 'commissionAsset': 'BNB', 'tradeId': 2379384578}], 'selfTradePreventionMode': 'NONE'}
What I want is withdraw float only from;
{'price': '16825.96000000'
as a float
myFloat = 16825.96000000
Mind you there are two ‘price’ one is 0.00000 the other is 16825.96000000
What you are getting is not a tuple but a dictionary. Let’s say the dictionary is called my_dic
. Then what you want is this:
my_price = my_dic["fills"][0]["price"]
Because my_dic["fills"]
will give you a list, of which the first element (index 0) is again a dictionary.
Note that this will be a string, to turn it into a number you will want to do float(my_price)