【问题标题】:Capture python value from multi-nested dictionary从多嵌套字典中捕获 python 值
【发布时间】:2018-07-17 20:30:43
【问题描述】:

我在从多嵌套字典中获取变量时遇到了一些问题。

我正在尝试使用以下 sn-p 获取 CategoryParentID

print dicstr['CategoryParentID']

而我的字典看起来像这样:

{
    "CategoryCount": "12842",
    "UpdateTime": "2018-04-10T02:31:49.000Z",
    "Version": "1057",
    "Ack": "Success",
    "Timestamp": "2018-07-17T18:33:40.893Z",
    "CategoryArray": {
        "Category": [
            {
                "CategoryName": "Antiques",
                "CategoryLevel": "1",
                "AutoPayEnabled": "true",
                "BestOfferEnabled": "true",
                "CategoryParentID": "20081",
                "CategoryID": "20081"
            },
.
.
.
}

我收到了这个错误,但是:

Traceback (most recent call last):
  File "get_categories.py", line 25, in <module>
    getUser()
  File "get_categories.py", line 18, in getUser
    print dictstr['CategoryParentID']
KeyError: 'CategoryParentID'

【问题讨论】:

    标签: python sdk ebay-api


    【解决方案1】:

    你需要先遍历字典。 CategoryParentID 在一个列表中(因此是[0]),其值为Category,其值为CategoryArray

    dicstr = {'CategoryCount': '12842',
     'UpdateTime': '2018-04-10T02:31:49.000Z',
     'Version': '1057',
     'Ack': 'Success',
     'Timestamp': '2018-07-17T18:33:40.893Z',
     'CategoryArray': {'Category': [{'CategoryName': 'Antiques',
        'CategoryLevel': '1',
        'AutoPayEnabled': 'true',
        'BestOfferEnabled': 'true',
        'CategoryParentID': '20081',
        'CategoryID': '20081'}]
          }
      }
    
    dicstr['CategoryArray']['Category'][0]['CategoryParentID']
    '20081'
    

    【讨论】:

      【解决方案2】:

      你必须得到它喜欢 -

      x['CategoryArray']['Category'][0]['CategoryParentID']
      

      如果我们简化您发布的字典,我们会得到 -

      d = {'CategoryArray': {'Category': [{'CategoryParentID': '20081'}]}}
      # "CategoryArray" 
      # "Category" child of CategoryArray 
      #  Key "Category" contains a list [{'CategoryName': 'Antiques', 'CategoryParentID': '20081'...}]
      #  Get 0th element of key "Category" and get value of key ["CategoryParentID"]
      

      我希望它有意义

      【讨论】:

        【解决方案3】:

        当你要求 Python 给你 dicstr['CategoryParentID'] 时,你要求它做的是给你与 dicstr 中的键 'CategoryParentID' 关联的值。

        查看您的字典的定义方式。 dictstr 的键将是所有恰好在dictstr 下一级的键。这些是 Python 在您指定 dictstr['CategoryParentID'] 时尝试查找 CategoryParentID 的键。这些键是:

        "CategoryCount"

        "UpdateTime"

        "Version"

        "Ack"

        "Timestamp"

        "CategoryArray"

        请注意,您要查找的密钥不存在。那是因为它在dictstr 中嵌套了几个层次。您需要继续“跳动”这些键,直到找到 CategoryParentID 键。试试:

        dictstr['CategoryArray']['Category'][0]['CategoryParentID']

        请注意其中的[0]。与'Category' 键关联的值是一个列表。该列表包含一个元素,即字典。为了获取包含您实际想要的键的字典,您必须在保存字典的索引处索引列表。由于只有一个元素,因此使用[0] 直接对其进行索引以获取字典,然后继续“跳”键。

        【讨论】:

          猜你喜欢
          • 2022-10-07
          • 1970-01-01
          • 2020-10-01
          • 1970-01-01
          • 2014-07-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多