如何将多个数组属性值放入csv fi

2024-10-01 11:27:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我有下面的样本数据。我的代码如下

代码:

样本数据:

{
  "data": [
  {
      "IdNo": "1234",
      "Name": "ABC",
       "Distribution": {
          "Section": [
            {
              "SectionName": {
                "id": "S1234"
              }
            }
          ],
          "Identity": [
            {
              "SectionName": {
                "id": "S5678"
              }
            },
            {
              "SectionName": {
                "id": "S7878"
              }
            }
          ]
          }

     }
     ]
}

df = pd.DataFrame()
for i in range(len(json_file['data'])):
    temp = {}
    temp['IdNo'] = json_file['data'][i]['IdNo']
    temp['Name'] = json_file['data'][i]['Name']
    for key in json_file['data'][i]['Distribution'].keys():
        try:
            for j in range(len(json_file['data'][i]['Distribution'][key])):
                temp[key]  = json_file['data'][i]['Distribution'][key][j]['SectionName']['id'] 
        except:
            temp[key] = None                    
        temp_df = pd.DataFrame([temp])
        df = pd.concat([df, temp_df], sort=True)        

我的输出只有一张唱片

IdNo     Name       Section     Identity
1234     Abc        S1234       S7878

请帮助我与任何建议,我如何才能实现以下输出 但我正在努力实现低于预期的产出

IdNo     Name           Section         Identity
1234     Abc            S1234           S5678
1234     Abc                            S7878

Tags: keynameidjsondfdatasectiontemp