从python中的嵌套元组获取值

2024-06-26 00:14:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个来自api的json响应,我需要从以下位置获取“askPrice”值:

dict_items(
    [
        ('SPY', 
            {
                 'assetType': 'ETF', 
                 'assetMainType': 'EQUITY', 
                 'cusip': '78462F103', 
                 'assetSubType': 'ETF', 
                 'symbol': 'SPY', 
                 'description': 'SPDR S&P 500', 
                 'bidPrice': 435.31, 
                 'bidSize': 2100, 
                 'bidId': 'T', 
                 'askPrice': 435.33, 
                 'askSize': 200, 
                 'askId': 'P', 
                 'lastPrice': 435.32, 
                 'lastSize': 0, 
                 ... , 
                 'delayed': True, 
                 'realtimeEntitled': False
             }
         )
     ]
 )

我的代码是:

quote = c.get_quotes("SPY")
resp = quote.json().items()
print(pd.json_normalize(resp['SPY']['AskPrice'],level=2))

但是python解释器显示错误:

TypeError: 'dict_items' object is not subscriptable

我对这个简单的问题深表歉意


Tags: apijsonitemssymbolrespdictquotespy