Python - 尝试从 API 查询读取 JSON 文件时出现问题



我正在查询NIH报告器API(https://api.reporter.nih.gov/),我能够获得格式化为JSON的输出。我的查询代码如下所示:

params={
"criteria":
{
"publicationsSearch": {
"publicationsTextSearch": "string",
"pmId": [
"21444817"
]
} 
}
,
"include_fields": [
"ApplId","SubprojectId","ProjectNum","FiscalYear", "AwardAmount"
],
"offset":0,
"limit":500,
"sort_order":"desc"
}

response = requests.post("https://api.reporter.nih.gov/v2/projects/search", json=params)
print(response.status_code)
print(response.text)

response.text返回如下所示的内容:

{">

meta":{"search_id":"-nePJuJYiUaI8MaRiLmp3Q","total":2663912,"offset":0,"limit":500,"sort_field":null,"sort_order":"desc","sorted_by_relevance":false,"properties":{"URL":"https:/reporter.nih.gov/search/-nePJuJYiUaI8MaRiLmp3Q/projects"}},"results":[{"appl_id":3949054,"subproject_id":"0230","fiscal_year":1987,"project_num":"5M01RR000240-23","award_amount":null},{"appl_id":7599150,"subproject_id":"9016","fiscal_year":2008,"project_num":"5P30CA043703-19","award_amount":205077}]}

我的最终目标是将其另存为数据帧。

为此,我正在尝试将request.text的输出保存为JSON文件,然后用pandas读取它(有更好的方法吗? 我的代码如下所示:

with open("sample.json", "w") as outfile:
json.dump(response.text, outfile)
df =pd.DataFrame("sample.json")

我的感觉是 sample.json 确实被保存了(我可以打开它,它看起来像一个 JSON 文件),但最后一行给我抛出了以下错误:"DataFrame 构造函数未正确调用!",老实说我不知道如何解决这个问题......

我将使用pandas.DataFrame.from_dict创建一个数据帧对象,而不是保存到磁盘并从磁盘读取。以下方法是否有效?

df = pd.DataFrame.from_dict(response.json()['results'])

它调用response.json(),它应该解析响应的文本并将其转换为 Pythondict对象。您可能还需要设置一些错误处理,以防数据检索失败或返回意外结果(例如,非 JSON 格式的文本可能会导致 response.json() 失败)。

最新更新