i just started up using phython & i try to help my dad to develop a small program:
from a query i get a retun that i converted into a dict as i want to extract the 'last_price': '1704.50'. I tried it with find, count ... but it seems that i'm running into a wall.
here is the string & it looks like that the bracket makes it harder to extract:
dict_items([('ret_code', 0), ('ret_msg', 'OK'), ('ext_code', ''), ('ext_info', ''), ('result', [{'symbol': 'ETHUSDT', 'bid_price': '1701.8', 'ask_price': '1702', 'last_price': '1704.50', 'last_tick_direction': 'PlusTick', 'prev_price_24h': '1647.55', 'price_24h_pcnt': '0.034566', 'high_price_24h': '1710.55', 'low_price_24h': '1620.40', 'prev_price_1h': '1679.40', 'price_1h_pcnt': '0.014945', 'mark_price': '1702.51', 'index_price': '1702.38', 'open_interest': 150155.81, 'open_value': '0.00', 'total_turnover': '54418340521.33', 'turnover_24h': '63239638.53', 'total_volume': 58525803.73, 'volume_24h': 37976.79, 'funding_rate': '0.0001', 'predicted_funding_rate': '0.0001', 'next_funding_time': '2022-08-25T08:00:00Z', 'countdown_hour': 1, 'delivery_fee_rate': '', 'predicted_delivery_price': '', 'delivery_time': ''}]), ('time_now', '1661411154.330653')])
Any idea is more than welcome
Thanks Alice
CodePudding user response:
Hope I'm uderstanding it right but this works for me:
d=dict([('ret_code', 0), ('ret_msg', 'OK'), ('ext_code', ''), ('ext_info', ''), ('result', [{'symbol': 'ETHUSDT', 'bid_price': '1701.8', 'ask_price': '1702', 'last_price': '1704.50', 'last_tick_direction': 'PlusTick', 'prev_price_24h': '1647.55', 'price_24h_pcnt': '0.034566', 'high_price_24h': '1710.55', 'low_price_24h': '1620.40', 'prev_price_1h': '1679.40', 'price_1h_pcnt': '0.014945', 'mark_price': '1702.51', 'index_price': '1702.38', 'open_interest': 150155.81, 'open_value': '0.00', 'total_turnover': '54418340521.33', 'turnover_24h': '63239638.53', 'total_volume': 58525803.73, 'volume_24h': 37976.79, 'funding_rate': '0.0001', 'predicted_funding_rate': '0.0001', 'next_funding_time': '2022-08-25T08:00:00Z', 'countdown_hour': 1, 'delivery_fee_rate': '', 'predicted_delivery_price': '', 'delivery_time': ''}]), ('time_now', '1661411154.330653')])
d['result'][0]['last_price']
gives the number. Notice the [0] due to the "[ ]" you mentioned.
CodePudding user response:
Hope I'm uderstanding it right, but you can use the get() function of dict. For example in your case:
dict_items = ([('ret_code', 0), ('ret_msg', 'OK'), ('ext_code', ''), ('ext_info', ''), ('result', [{'symbol': 'ETHUSDT', 'bid_price': '1701.8', 'ask_price': '1702', 'last_price': '1704.50', 'last_tick_direction': 'PlusTick', 'prev_price_24h': '1647.55', 'price_24h_pcnt': '0.034566', 'high_price_24h': '1710.55', 'low_price_24h': '1620.40', 'prev_price_1h': '1679.40', 'price_1h_pcnt': '0.014945', 'mark_price': '1702.51', 'index_price': '1702.38', 'open_interest': 150155.81, 'open_value': '0.00', 'total_turnover': '54418340521.33', 'turnover_24h': '63239638.53', 'total_volume': 58525803.73, 'volume_24h': 37976.79, 'funding_rate': '0.0001', 'predicted_funding_rate': '0.0001', 'next_funding_time': '2022-08-25T08:00:00Z', 'countdown_hour': 1, 'delivery_fee_rate': '', 'predicted_delivery_price': '', 'delivery_time': ''}]), ('time_now', '1661411154.330653')])
print(dict_items.get('last_price'))
# Output: 1704.50
CodePudding user response:
If you are dealing with texts, regex might be more appropriate:
import re
text = """dict_items([('ret_code', 0), ('ret_msg', 'OK'), ('ext_code', ''), ('ext_info', ''), ('result', [{'symbol': 'ETHUSDT', 'bid_price': '1701.8', 'ask_price': '1702', 'last_price': '1704.50', 'last_tick_direction': 'PlusTick', 'prev_price_24h': '1647.55', 'price_24h_pcnt': '0.034566', 'high_price_24h': '1710.55', 'low_price_24h': '1620.40', 'prev_price_1h': '1679.40', 'price_1h_pcnt': '0.014945', 'mark_price': '1702.51', 'index_price': '1702.38', 'open_interest': 150155.81, 'open_value': '0.00', 'total_turnover': '54418340521.33', 'turnover_24h': '63239638.53', 'total_volume': 58525803.73, 'volume_24h': 37976.79, 'funding_rate': '0.0001', 'predicted_funding_rate': '0.0001', 'next_funding_time': '2022-08-25T08:00:00Z', 'countdown_hour': 1, 'delivery_fee_rate': '', 'predicted_delivery_price': '', 'delivery_time': ''}]), ('time_now', '1661411154.330653')])"""
last_price_pattern = r"'last_price'\s*:\s*'(\d*\.\d*)'"
last_price = float(re.findall(last_price_pattern, text)[0])
print(last_price)
If you're dealing with a list, then simple indexing will suffice:
raw_data = [('ret_code', 0), ('ret_msg', 'OK'), ('ext_code', ''), ('ext_info', ''), ('result', [{'symbol': 'ETHUSDT', 'bid_price': '1701.8', 'ask_price': '1702', 'last_price': '1704.50', 'last_tick_direction': 'PlusTick', 'prev_price_24h': '1647.55', 'price_24h_pcnt': '0.034566', 'high_price_24h': '1710.55', 'low_price_24h': '1620.40', 'prev_price_1h': '1679.40', 'price_1h_pcnt': '0.014945', 'mark_price': '1702.51', 'index_price': '1702.38', 'open_interest': 150155.81, 'open_value': '0.00', 'total_turnover': '54418340521.33', 'turnover_24h': '63239638.53', 'total_volume': 58525803.73, 'volume_24h': 37976.79, 'funding_rate': '0.0001', 'predicted_funding_rate': '0.0001', 'next_funding_time': '2022-08-25T08:00:00Z', 'countdown_hour': 1, 'delivery_fee_rate': '', 'predicted_delivery_price': '', 'delivery_time': ''}]), ('time_now', '1661411154.330653')]
last_price = float(raw_data[4][1][0]['last_price'])
print(last_price)