【问题标题】:Accessing data in a nested dict? Python [duplicate]访问嵌套字典中的数据? Python [重复]
【发布时间】:2020-10-03 10:27:54
【问题描述】:

我从响应中得到以下数据。这似乎是一个字典。

例如,我可以访问“说明”。您能否建议我应该阅读哪些内容以允许我访问“tcp_options”位。我假设这是一个嵌套的字典。

  {
  "description": "sftp",
  "icmp_options": null,
  "is_stateless": false,
  "protocol": "6",
  "source": "127.0.0.1/32",
  "source_type": "CIDR_BLOCK",
  "tcp_options": {
    "destination_port_range": null,
    "source_port_range": {
      "max": 5500,
      "min": 5500
    }
  },
  "udp_options": null
  },

【问题讨论】:

  • 你说你可以访问"description",也就是说你可以访问"tcp_options"。有没有你还没有提到的问题?
  • 所有,特别是 quamrana。是的,我想我应该在尝试访问我得到的“tcp_options”时提到。 KeyError:'tcp_options'。对不起。

标签: python variables


【解决方案1】:

全部,

首先感谢您的投入。但事实证明我有点白痴。

'tcp_options"':

实际上那里有一个“,这就是我得到错误的原因。我认为”是某种形式的神秘蟒蛇巫毒。

对不起,谢谢大家。

edit:我的代码行:print(newRules['tcp_options"']['destination_port_range']['min']) 现在可以正常工作了。

【讨论】:

    【解决方案2】:

    通过其键值访问dict 应该没问题:

    dd = {
      "description": "sftp",
      "icmp_options": 'null',
      "is_stateless": 'false',
      "protocol": "6",
      "source": "127.0.0.1/32",
      "source_type": "CIDR_BLOCK",
      "tcp_options": {
        "destination_port_range": 'null',
        "source_port_range": {
          "max": 5500,
          "min": 5500
        }
      },
      "udp_options": 'null'
      }
    
    for key,value in dd.items():
        print(key,value)
    
    
    print(dd["tcp_options"])
    

    输出:

    description sftp                                                                                                                                                             
    source_type CIDR_BLOCK                                                                                                                                                       
    protocol 6                                                                                                                                                                   
    source 127.0.0.1/32                                                                                                                                                          
    icmp_options null                                                                                                                                                            
    tcp_options {'source_port_range': {'max': 5500, 'min': 5500}, 'destination_port_range': 'null'}                                                                              
    is_stateless false                                                                                                                                                           
    udp_options null                                                                                                                                                             
    {'source_port_range': {'max': 5500, 'min': 5500}, 'destination_port_range': 'null'}
    

    【讨论】:

      【解决方案3】:

      在python中访问嵌套字典中的元素,如下所示:

      thing = { "hi":{"hello":"bye"}}
      

      你会写:

      print(thing["hi"]["hello"])
      

      这应该返回以下内容:

      >>> print(thing["hi"]["hello"])
      bye
      >>> ...
      

      希望这能回答你的问题!

      【讨论】:

      • 很好,谢谢。我可能应该添加我收到以下错误:KeyError:'tcp_options'
      • 我也尝试“竖起大拇指”你的帖子,但它不允许我。
      • 别担心,因为声望大于 15 的人做不到。
      猜你喜欢
      • 2018-02-07
      • 2012-05-11
      • 2022-01-17
      • 2014-06-11
      • 2018-02-09
      • 1970-01-01
      • 2020-08-28
      • 2013-09-29
      • 1970-01-01
      相关资源
      最近更新 更多