【问题标题】:Manipulating data from json to reflect a single value from each entry处理来自 json 的数据以反映每个条目的单个值
【发布时间】:2018-12-19 17:37:49
【问题描述】:

设置:

这个数据集有 50 个“问题”,在这些“问题”中,我已经捕获了我需要然后放入我的 postgresql 数据库的数据。但是当我到达“组件”时,我遇到了麻烦。我能够获得“组件”的所有“名称”的列表,但只想为每个“问题”拥有 1 个“名称”实例,其中一些有 2 个。有些是空的,想为那些。

以下是一些足够的示例数据:

{
 "issues": [
    {
      "key": "1",
      "fields": {
        "components": [],
        "customfield_1": null,
        "customfield_2": null
      }
    },
    {
      "key": "2",
      "fields": {
        "components": [
            {
              "name": "Testing"
            }
          ],
        "customfield_1": null,
        "customfield_2": null
      }
    },
    {
      "key": "3",
      "fields": {
         "components": [
            {
              "name": "Documentation"
            },
            {
               "name": "Manufacturing"
            }
           ],
          "customfield_1": null,
          "customfield_2": 5
      }
     }
  ]
 }

我正在寻找返回(仅用于组件名称):

['null', 'Testing', 'Documentation'] 

我将其他数据设置为进入数据库,如下所示:

values = list((item['key'],
               //components list,
               item['fields']['customfield_1'],
               item['fields']['customfield_2']) for item in data_story['issues'])

我想知道是否有一种可能的方法可以进入我在上面评论过“组件列表”的已创建组件列表中

只是为了回顾一下,我希望每个问题只有 1 个组件名称是否为 null,并且能够将其与其余数据一起放入 values 变量中。组件中的第一个 name 也适用于每个“问题”

【问题讨论】:

  • “只希望每个“问题”有 1 个“名称”实例”是什么意思。你想去掉一个还是展平它,以便一个“名称”返回两个值?
  • 我希望一个不被放入列表中,所以对于每个“问题”我只得到一个组件名称

标签: python json python-3.x


【解决方案1】:

假设我们正在使用data 变量,我会这样做:

values = [(x['fields']['components'][0]['name'] if len(x['fields']['components']) != 0 else 'null') for x in data['issues']]

如果您有任何疑问,请告诉我。

【讨论】:

  • 这可行,但为了完整起见,我会将value 变量的“//组件列表”部分设为:item['fields']['components'][0]['name'] if len(item['fields']['components']) != 0 else 'null'
【解决方案2】:

dict comprehension 中使用if/else 示例代码是

results = [ (x['fields']['components'][0]['name'] if 'components' in x['fields'] and len(x['fields']['components']) > 0 else 'null') for x in data['issues'] ]

完整的示例代码是

import json
data = json.loads('''{ "issues": [
{
    "key": "1",
    "fields": {
        "components": [],
        "customfield_1": null,
        "customfield_2": null
    }
},
{
    "key": "2",
    "fields": {
        "components": [
                {
                    "name": "Testing"
                }
            ],
        "customfield_1": null,
        "customfield_2": null
    }
},
{
    "key": "3",
    "fields": {
         "components": [
                {
                    "name": "Documentation"
                },
                {
                     "name": "Manufacturing"
                }
             ],
            "customfield_1": null,
            "customfield_2": 5
    }
 }
]
}''')

results = [ (x['fields']['components'][0]['name'] if 'components' in x['fields'] and len(x['fields']['components']) > 0 else 'null') for x in data['issues'] ]
print(results)

输出是['null', u'Testing', u'Documentation']

【讨论】:

    【解决方案3】:

    如果您只想从列表中删除除一个之外的所有名称,那么您可以这样做:

    issues={
     "issues": [
        {
          "key": "1",
          "fields": {
            "components": [],
            "customfield_1": "null",
            "customfield_2": "null"
          }
        },
        {
          "key": "2",
          "fields": {
            "components": [
                {
                  "name": "Testing"
                }
              ],
            "customfield_1": "null",
            "customfield_2": "null"
          }
        },
        {
          "key": "3",
          "fields": {
             "components": [
                {
                  "name": "Documentation"
                },
                {
                   "name": "Manufacturing"
                }
               ],
              "customfield_1": "null",
              "customfield_2": 5
          }
         }
      ]
     }
    

    数据^

    componentlist=[]
    for i in range(len(issues["issues"])):
        x= issues["issues"][i]["fields"]["components"]
        if len(x)==0:
            x="null"
            componentlist.append(x)
        else:
            x=issues["issues"][i]["fields"]["components"][0]
            componentlist.append(x)
    
    print(componentlist)
    
    >>>['null', {'name': 'Testing'}, {'name': 'Documentation'}]
    

    或者,如果您只想要值,而不是字典键:

    else:
        x=issues["issues"][i]["fields"]["components"][0]["name"]
        componentlist.append(x)
    
    ['null', 'Testing', 'Documentation']
    

    【讨论】:

      猜你喜欢
      • 2019-06-22
      • 1970-01-01
      • 1970-01-01
      • 2011-04-24
      • 2019-07-20
      • 1970-01-01
      • 2019-03-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多