以下代码从服务总线接收信息并打印信息。
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"one_answers"ADS_FND_MSF620">
我的尝试如下:
result['mappings']['table']
但我得到了类型错误:
TypeError: list indices must be integers or slices, not str
我知道这只是一段我应该知道的python代码,但任何想法都欢迎
出现此错误的原因是列表总是需要带整数的索引。出现错误-当字典在列表中时,您试图使用键访问字典,因此会出现此错误。如果紧跟其后,则键"table"在字典"ELLIPSE"中,并且它存在于父列表的第一个索引位置。
因此,尝试访问以下字典密钥-
result['mappings'][0]['ELLIPSE']['tables'][0]['schema']
类似地,
result['mappings'][0]['ELLIPSE']['tables'][0]['table']