Python 如果密钥属性不存在,则在解析json文件时获取0条记录

Python 如果密钥属性不存在,则在解析json文件时获取0条记录,python,json,pandas,csv,dataframe,Python,Json,Pandas,Csv,Dataframe,我有几个静态键列EmployeeId、type和几个来自first FOR循环的列 而在第二个FOR循环中,如果我有一个特定的键,那么只有值应该附加到现有的数据帧列,否则从第一个FOR循环获取的列应该保持不变 循环输出的第一个: "EmployeeId","type","KeyColumn","Start","End","Country","Target","CountryId","TargetId" "Emp1","Metal","1212121212","2000-06-17","9999-

我有几个静态键列EmployeeId、type和几个来自first FOR循环的列

而在第二个FOR循环中,如果我有一个特定的键,那么只有值应该附加到现有的数据帧列,否则从第一个FOR循环获取的列应该保持不变

循环输出的第一个:

"EmployeeId","type","KeyColumn","Start","End","Country","Target","CountryId","TargetId"
"Emp1","Metal","1212121212","2000-06-17","9999-12-31","","","",""
"EmployeeId","type","KeyColumn","Start","End","Country","Target","CountryId","TargetId"
"Emp1","Metal","1212121212","2000-06-17","9999-12-31","","AMAZON","1",""
"Emp1","Metal","1212121212","2000-06-17","9999-12-31","","FLIPKART","2",""
在第二个For循环之后,我有以下输出:

"EmployeeId","type","KeyColumn","Start","End","Country","Target","CountryId","TargetId"
"Emp1","Metal","1212121212","2000-06-17","9999-12-31","","","",""
"EmployeeId","type","KeyColumn","Start","End","Country","Target","CountryId","TargetId"
"Emp1","Metal","1212121212","2000-06-17","9999-12-31","","AMAZON","1",""
"Emp1","Metal","1212121212","2000-06-17","9999-12-31","","FLIPKART","2",""
根据代码,如果我有Employee标记可用,我有2条以上的记录,但我可能有几个没有Employee标记的json文件,那么输出应该与第一次循环输出相同,所有键字段都填充,其余列都为null

但根据我的代码,我得到了0条记录。如果我的编码方式不对,请帮助我

请帮帮我。。。如果问问题的方式不清楚,我很抱歉,因为我是python新手。请在下面的链接中找到示例数据

请查找以下代码

    for i in range(len(json_file['enty'])):
        temp = {}
        temp['EmployeeId'] = json_file['enty'][i]['id']
        temp['type'] = json_file['enty'][i]['type']
        for key in json_file['enty'][i]['data']['attributes'].keys():        
            try:
                temp[key] = json_file['enty'][i]['data']['attributes'][key]['values'][0]['value']
            except:
                temp[key] = None      

        for key in json_file['enty'][i]['data']['attributes'].keys(): 
            if(key == 'Employee'):
                for j in range(len(json_file['enty'][i]['data']['attributes']['Employee']['group'])):
                    for key in json_file['enty'][i]['data']['attributes']['Employee']['group'][j].keys():
                        try:
                            temp[key] = json_file['enty'][i]['data']['attributes']['Employee']['group'][j][key]['values'][0]['value']
                        except:
                            temp[key] = None

                    temp_df = pd.DataFrame([temp])
                    df = pd.concat([df, temp_df], sort=True)

    # Rearranging columns
    df = df[['EmployeeId', 'type'] + [col for col in df.columns if col not in ['EmployeeId', 'type']]]

    # Writing the dataset
    df[columns_list].to_csv("Test22.csv", index=False, quotechar='"', quoting=1)
如果Employee标记不可用,我将获得0条记录作为输出,但我希望1条记录作为第一个for循环


JSON结构相当复杂。我试图从中简化数据收集。结果是一个简单的dict列表。该代码处理未找到“Employee”的情况

import copy

d = {
    "enty": [
        {
            "id": "Emp1",
            "type": "Metal",
            "data": {
                "attributes": {
                    "KeyColumn": {
                        "values": [
                            {
                                "value": 1212121212
                            }
                        ]
                    },
                    "End": {
                        "values": [
                            {
                                "value": "2050-12-31"
                            }
                        ]
                    },
                    "Start": {
                        "values": [
                            {
                                "value": "2000-06-17"
                            }
                        ]
                    },
                    "Employee": {
                        "group": [
                            {
                                "Target": {
                                    "values": [
                                        {
                                            "value": "AMAZON"
                                        }
                                    ]
                                },
                                "CountryId": {
                                    "values": [
                                        {
                                            "value": "1"
                                        }
                                    ]
                                }
                            },
                            {
                                "Target": {
                                    "values": [
                                        {
                                            "value": "FLIPKART"
                                        }
                                    ]
                                },
                                "CountryId": {
                                    "values": [
                                        {
                                            "value": "2"
                                        }
                                    ]
                                }
                            }
                        ]
                    }
                }
            }
        }
    ]
}
emps = []
for e in d['enty']:
    entry = {'id': e['id'], 'type': e['type']}
    for x in ["KeyColumn", "Start", "End"]:
        entry[x] = e['data']['attributes'][x]['values'][0]['value']
    if e['data']['attributes'].get('Employee'):
        for grp in e['data']['attributes']['Employee']['group']:
            clone = copy.deepcopy(entry)
            for x in ['Target', 'CountryId']:
                clone[x] = grp[x]['values'][0]['value']
            emps.append(clone)
    else:
        emps.add(entry)
# TODO write to csv
for emp in emps:
    print(emp) 
输出

{'End': '2050-12-31', 'Target': 'AMAZON', 'KeyColumn': 1212121212, 'Start': '2000-06-17', 'CountryId': '1', 'type': 'Metal', 'id': 'Emp1'}
{'End': '2050-12-31', 'Target': 'FLIPKART', 'KeyColumn': 1212121212, 'Start': '2000-06-17', 'CountryId': '2', 'type': 'Metal', 'id': 'Emp1'}

JSON结构相当复杂。我试图从中简化数据收集。结果是一个简单的dict列表。该代码处理未找到“Employee”的情况

import copy

d = {
    "enty": [
        {
            "id": "Emp1",
            "type": "Metal",
            "data": {
                "attributes": {
                    "KeyColumn": {
                        "values": [
                            {
                                "value": 1212121212
                            }
                        ]
                    },
                    "End": {
                        "values": [
                            {
                                "value": "2050-12-31"
                            }
                        ]
                    },
                    "Start": {
                        "values": [
                            {
                                "value": "2000-06-17"
                            }
                        ]
                    },
                    "Employee": {
                        "group": [
                            {
                                "Target": {
                                    "values": [
                                        {
                                            "value": "AMAZON"
                                        }
                                    ]
                                },
                                "CountryId": {
                                    "values": [
                                        {
                                            "value": "1"
                                        }
                                    ]
                                }
                            },
                            {
                                "Target": {
                                    "values": [
                                        {
                                            "value": "FLIPKART"
                                        }
                                    ]
                                },
                                "CountryId": {
                                    "values": [
                                        {
                                            "value": "2"
                                        }
                                    ]
                                }
                            }
                        ]
                    }
                }
            }
        }
    ]
}
emps = []
for e in d['enty']:
    entry = {'id': e['id'], 'type': e['type']}
    for x in ["KeyColumn", "Start", "End"]:
        entry[x] = e['data']['attributes'][x]['values'][0]['value']
    if e['data']['attributes'].get('Employee'):
        for grp in e['data']['attributes']['Employee']['group']:
            clone = copy.deepcopy(entry)
            for x in ['Target', 'CountryId']:
                clone[x] = grp[x]['values'][0]['value']
            emps.append(clone)
    else:
        emps.add(entry)
# TODO write to csv
for emp in emps:
    print(emp) 
输出

{'End': '2050-12-31', 'Target': 'AMAZON', 'KeyColumn': 1212121212, 'Start': '2000-06-17', 'CountryId': '1', 'type': 'Metal', 'id': 'Emp1'}
{'End': '2050-12-31', 'Target': 'FLIPKART', 'KeyColumn': 1212121212, 'Start': '2000-06-17', 'CountryId': '2', 'type': 'Metal', 'id': 'Emp1'}

什么是json文件?你的预期产出是多少?您的实际输出是2条记录还是0条记录?@Goyo。。非常感谢高雄的回复。。我在上面附上了示例json文件。实际上,在json文件中的每个记录中都有几个键列。有时键属性将不可用,然后我将获取记录,但至少应该填充像Employee Id和type这样的键列,其余列可以保持空白。但是,如果“Employee”标记不可用,我无法在输出csv文件中获取任何记录。@Goyo。。Json文件是我用来通过python解析它们并创建csv输出文件的源文件。请帮助我如何解决这个问题。@Goyo。。请帮我做个手势1.恐怕我帮不了什么忙。我仍然不确定您期望的输出是什么,代码太复杂,我无法理解,我无法运行和调试它,因为有太多未定义的东西。我能提出的最好的建议是你发布一个.json\u文件是什么?你的预期产出是多少?您的实际输出是2条记录还是0条记录?@Goyo。。非常感谢高雄的回复。。我在上面附上了示例json文件。实际上,在json文件中的每个记录中都有几个键列。有时键属性将不可用,然后我将获取记录,但至少应该填充像Employee Id和type这样的键列,其余列可以保持空白。但是,如果“Employee”标记不可用,我无法在输出csv文件中获取任何记录。@Goyo。。Json文件是我用来通过python解析它们并创建csv输出文件的源文件。请帮助我如何解决这个问题。@Goyo。。请帮我做个手势1.恐怕我帮不了什么忙。我仍然不确定您期望的输出是什么,代码太复杂,我无法理解,我无法运行和调试它,因为有太多未定义的东西。我能提出的最好的建议是你发一个帖子。