Pyton脚本只从json导出一半的信息



我有python脚本,但当导出文件时只打印"y", " measure ", "label"没有任何数据。运行脚本时没有错误。你能建议解决这个问题吗?我不能发布链接和json.

import requests
from pandas.io.json import json_normalize
import json
import pandas as pd
import numpy as np
# try this
total_df = pd.DataFrame()
request_list = ["LInk",]
# read each link
for r in request_list:
resp = requests.get(url=r)
df = json_normalize(resp.json())
item_list = df['dataset']
current_df = pd.DataFrame()


for i in item_list:
try:
current_df.loc[0,'y'] = i['data'][0]['y']
except:
current_df.loc[0,'y'] = np.nan
try:
current_df.loc[0,'meas'] = i['meas']
except:
current_df.loc[0,'meas'] = np.nan
try:
current_df.loc[0,'label'] = i['label']
except:
current_df.loc[0,'label'] = np.nan
total_df = pd.concat([total_df,current_df])

total_df.to_excel('C:/Users/svetl/Onedrive/Desktop/work/output.xlsx',index=False)  

所以问题是你处理JSON的方式。键dataset有一个字典项列表作为值。您尝试直接访问dict项,而忽略它们是列表的一部分。

查看下面编辑的部分,并注意从
i['data'][0]['y']

i[0]['data'][0]['y']
的变化(以及访问i时的其他变化)

for i in item_list:
try:
current_df.loc[0,'y'] = i[0]['data'][0]['y']
except Exception as e:
print(e)
current_df.loc[0,'y'] = np.nan
try:
current_df.loc[0,'meas'] = i[0]['meas']
except Exception as e:
print(e)
current_df.loc[0,'meas'] = np.nan
try:
current_df.loc[0,'label'] = i[0]['label']
except Exception as e:
print(e)
current_df.loc[0,'label'] = np.nan

旁注:

已弃用:

from pandas.io.json import json_normalize

你需要把它改成:

from pandas import json_normalize

最新更新