[英]How to pull out value from Key:Value Pair with Databricks Notebook
The following code recieves information from Service Bus and prints the information.以下代码从服务总线接收信息并打印信息。
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
I then assign a variable to the function as follows:然后我将一个变量分配给 function,如下所示:
result = myfunc()
When I enter the following code当我输入以下代码时
result['mappings']
I get the following output:我得到以下 output:
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',
I am now trying to extract 'AZ_FH_ELLIPSE' and 'ADS_FND_MSF620'我现在正在尝试提取“AZ_FH_ELLIPSE”和“ADS_FND_MSF620”
My attempt is as follows:我的尝试如下:
result['mappings']['table']
But I get the Typeerror:但是我得到了类型错误:
TypeError: list indices must be integers or slices, not str
I'm aware this is simply piece of python code I should know, but any thoughts welcomed我知道这只是我应该知道的 python 代码的一部分,但欢迎任何想法
The reason you're getting this error is because list always expects index with integers.您收到此错误的原因是因为 list 总是需要整数索引。 Coming to the error - This error occurs because you have tried to access the dictionary using the key, when the dictionary is inside of a list.
出现错误 - 发生此错误是因为当字典位于列表内部时,您尝试使用键访问字典。 If you follow closely then key 'table' is within the dictionary 'ELLIPSE' and it exists at the first index position of the parent list.
如果您仔细观察,那么键“table”在字典“ELLIPSE”中,并且它存在于父列表的第一个索引 position 中。
Thereby, try accessing the dictionary keys as below -因此,尝试访问字典键如下 -
result['mappings'][0]['ELLIPSE']['tables'][0]['schema']
Similarly,相似地,
result['mappings'][0]['ELLIPSE']['tables'][0]['table']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.