【问题标题】:How to parse particular values from string of dictionaries [duplicate]如何解析字典字符串中的特定值[重复]
【发布时间】:2019-12-15 13:39:38
【问题描述】:

我无法使用 python 从字典字符串中解析项目:

[
  {
    "name": "abc",  
    "channel": 44,
    "band": 12 
    }, 
  {
    "name": "abcd",  
    "channel": 45, 
    "band": 1
    },
  {
    "name": "ab",  
    "channel": 46,
    "band": 4 
    },
  {
    "name": "abc1",  
    "channel": 44, 
     "band": 15
    },
 {
    "name": "abc12",  
    "channel": 44, 
     "band": 11
    },
.
.

希望仅从频道 =44 的字典中获取频道和名称

预期输出:

'name': 'abc', 'channel': 44
'name': 'abc1', 'channel': 44
'name': 'abc12', 'channel': 44

请帮忙!

【问题讨论】:

    标签: python string dictionary parsing


    【解决方案1】:
    output = []
    for item in items:
        if item['channel'] == 44:
            output.append(item['name'], item['channel'])
    
    

    items 是您的字典列表。 您需要的所有数据都将在输出

    【讨论】:

    • @aapg07 尝试关注:res_data = response.json()
    • 首先,您应该将其包含在您的问题中。但还是。尝试打印数据并查看其结构,然后调整您的条件。
    • 这对我有用.. 谢谢!
    【解决方案2】:
    dictionary = [
      {
        "name": "abc",  
        "channel": 44,
        "band": 12 
        }, 
      {
        "name": "abcd",  
        "channel": 45, 
        "band": 1
        },
      {
        "name": "ab",  
        "channel": 46,
        "band": 4 
        },
      {
        "name": "abc1",  
        "channel": 44, 
         "band": 15
        },
     {
        "name": "abc12",  
        "channel": 44, 
         "band": 11
        }
    ]
    
    
    for element in dictionary:
        if element['channel'] == 44:
            print('\'name\': {0}, \'channel\': {1}'.format(element['name'], element['channel']))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-06-13
      • 2021-09-11
      • 1970-01-01
      • 1970-01-01
      • 2015-02-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多