如何从 Key:Value 与 Databricks Notebook 配对中提取价值

How to pull out value from Key:Value Pair with Databricks Notebook

以下代码从服务总线接收信息并打印信息。

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 我应该知道的代码,但欢迎任何想法

您收到此错误的原因是列表始终需要整数索引。 出现错误 - 发生此错误是因为当字典位于列表内部时,您尝试使用键访问字典。如果您仔细观察,那么键 'table' 在字典 'ELLIPSE' 中并且它存在于父列表的第一个索引位置。

因此,尝试访问字典键如下 -

result['mappings'][0]['ELLIPSE']['tables'][0]['schema']

同样,

result['mappings'][0]['ELLIPSE']['tables'][0]['table']