gpt4 book ai didi

python - 如果键属性不存在,则解析 json 文件时获取 0 条记录

转载 作者:太空宇宙 更新时间:2023-11-03 20:43:34 26 4
gpt4 key购买 nike

我有几个静态键列 EmployeeId,type 和来自第一个 FOR 循环的几列。

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

第一个 For 循环输出:

"EmployeeId","type","KeyColumn","Start","End","Country","Target","CountryId","TargetId"
"Emp1","Metal","1212121212","2000-06-17","9999-12-31","","","",""

第二个 For 循环之后我有以下输出:

"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)

如果员工标签不可用,我将得到 0 条记录作为输出,但我期望第一个 for 循环有 1 条记录

enter link description here

最佳答案

JSON结构相当复杂。我尝试简化其中的数据收集。结果是一个平面字典列表。该代码处理未找到“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'}

关于python - 如果键属性不存在,则解析 json 文件时获取 0 条记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56727851/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com