格式化 JSON GET 结果为 Python
Formatting JSON GET results in Python
我正在尝试从 Europe Pubmed Central. The JSON results returned by Europe PMC server looks like this 获取 Covid-19 JSON 数据。
我查询服务器的初始代码如下所示:
import requests
import json
mydata = "https://www.ebi.ac.uk/europepmc/webservices/rest/search?query=(%E2%80%9C2019-nCoV%E2%80%9D)&format=json"
#get Server response
reply = requests.get(mydata)
#print out results
print(reply.json())
我希望摆脱 JSON 的这些部分:
{'version': '6.2', 'hitCount': 847, 'nextCursorMark': 'AoIIQVJxdCg0MTI2NjU3Mw==', 'request': {'queryString': '(“2019-nCoV”)', 'resultType': 'lite', 'cursorMark': '*', 'pageSize': 25, 'sort': '', 'synonym': False}, 'resultList':
如何删除 python 中的这一部分?对于长 url 查询字符串,我提前表示歉意。
我建议只做
reply = reply['resultList']
那么回复将只包含
{
"result": [
{
"id": "32036774",
"source": "MED",
"pmid": "32036774",
"pmcid": "PMC7054940",
"doi": "10.1080/01652176.2020.1727993",
"title": "Emerging novel coronavirus (2019-nCoV)-current scenario, evolutionary perspective based on genome analysis and recent developments.",
"authorString": "Malik YS, Sircar S, Bhat S, Sharun K, Dhama K, Dadar M, Tiwari R, Chaicumpa W.",
"journalTitle": "Vet Q",
"issue": "1",
"journalVolume": "40",
"pubYear": "2020",
"journalIssn": "0165-2176; 1875-5941; ",
"pageInfo": "68-76",
"pubType": "other; review; journal article",
"isOpenAccess": "Y",
"inEPMC": "Y",
"inPMC": "N",
"hasPDF": "Y",
"hasBook": "N",
"hasSuppl": "Y",
"citedByCount": 0,
"hasReferences": "N",
"hasTextMinedTerms": "Y",
"hasDbCrossReferences": "N",
"hasLabsLinks": "Y",
"hasTMAccessionNumbers": "Y",
"tmAccessionTypeList": {
"accessionType": [
"gen"
]
},
"firstIndexDate": "2020-02-11",
"firstPublicationDate": "2020-12-01"
}, ...
]
}
从那里你可以像这样遍历所有对象
for result in reply['results']:
print(result)
我正在尝试从 Europe Pubmed Central. The JSON results returned by Europe PMC server looks like this 获取 Covid-19 JSON 数据。
我查询服务器的初始代码如下所示:
import requests
import json
mydata = "https://www.ebi.ac.uk/europepmc/webservices/rest/search?query=(%E2%80%9C2019-nCoV%E2%80%9D)&format=json"
#get Server response
reply = requests.get(mydata)
#print out results
print(reply.json())
我希望摆脱 JSON 的这些部分:
{'version': '6.2', 'hitCount': 847, 'nextCursorMark': 'AoIIQVJxdCg0MTI2NjU3Mw==', 'request': {'queryString': '(“2019-nCoV”)', 'resultType': 'lite', 'cursorMark': '*', 'pageSize': 25, 'sort': '', 'synonym': False}, 'resultList':
如何删除 python 中的这一部分?对于长 url 查询字符串,我提前表示歉意。
我建议只做
reply = reply['resultList']
那么回复将只包含
{
"result": [
{
"id": "32036774",
"source": "MED",
"pmid": "32036774",
"pmcid": "PMC7054940",
"doi": "10.1080/01652176.2020.1727993",
"title": "Emerging novel coronavirus (2019-nCoV)-current scenario, evolutionary perspective based on genome analysis and recent developments.",
"authorString": "Malik YS, Sircar S, Bhat S, Sharun K, Dhama K, Dadar M, Tiwari R, Chaicumpa W.",
"journalTitle": "Vet Q",
"issue": "1",
"journalVolume": "40",
"pubYear": "2020",
"journalIssn": "0165-2176; 1875-5941; ",
"pageInfo": "68-76",
"pubType": "other; review; journal article",
"isOpenAccess": "Y",
"inEPMC": "Y",
"inPMC": "N",
"hasPDF": "Y",
"hasBook": "N",
"hasSuppl": "Y",
"citedByCount": 0,
"hasReferences": "N",
"hasTextMinedTerms": "Y",
"hasDbCrossReferences": "N",
"hasLabsLinks": "Y",
"hasTMAccessionNumbers": "Y",
"tmAccessionTypeList": {
"accessionType": [
"gen"
]
},
"firstIndexDate": "2020-02-11",
"firstPublicationDate": "2020-12-01"
}, ...
]
}
从那里你可以像这样遍历所有对象
for result in reply['results']:
print(result)