以下代碼從服務總線接收資訊并列印資訊。
def myfunc():
with ServiceBusClient.from_connection_string(CONNECTION_STR) as client:
# max_wait_time specifies how long the receiver should wait with no incoming messages before stopping receipt.
# Default is None; to receive forever.
with client.get_queue_receiver(QUEUE_NAME, session_id=session_id, max_wait_time=3600) as receiver:
for msg in receiver:
# print("Received: " str(msg))
themsg = json.loads(str(msg))
# complete the message so that the message is removed from the queue
receiver.complete_message(msg)
return themsg
然后我為函式分配一個變數,如下所示:
result = myfunc()
當我輸入以下代碼
result['mappings']
我得到以下輸出:
Out[45]: [{'ELLIPSE': {'method': 'ellipseItem',
'tables': [{'database': 'foundation',
'schema': 'AZ_FH_ELLIPSE',
'table': 'ADS_FND_MSF620',
'primaryKey': [{'column': 'DSTRCT_CODE', 'seqno': 1},
{'column': 'WORK_ORDER', 'seqno': 2}]}],
'columns': [{'column': 'D_WORK_ORDER_KEY',
我現在正在嘗試提取“AZ_FH_ELLIPSE”和“ADS_FND_MSF620”
我的嘗試如下:
result['mappings']['table']
但我得到了型別錯誤:
TypeError: list indices must be integers or slices, not str
我知道這只是我應該知道的一段 python 代碼,但歡迎提出任何想法
uj5u.com熱心網友回復:
您收到此錯誤的原因是因為 list 始終需要整數索引。出現錯誤 - 發生此錯誤是因為當字典位于串列內時,您嘗試使用鍵訪問字典。如果您密切關注,則鍵 'table' 位于字典 'ELLIPSE' 中,并且它存在于父串列的第一個索引位置。
因此,嘗試訪問字典鍵如下 -
result['mappings'][0]['ELLIPSE']['tables'][0]['schema']
相似地,
result['mappings'][0]['ELLIPSE']['tables'][0]['table']
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/436852.html
標籤:Python 阿帕奇火花 pyspark 天蓝色数据块
