Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
格式化JSON在Python中获得结果_Python_Json_Europepmc - Fatal编程技术网

格式化JSON在Python中获得结果

格式化JSON在Python中获得结果,python,json,europepmc,Python,Json,Europepmc,我正在尝试从中获取新冠病毒-19 JSON数据。欧洲PMC服务器返回的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

我正在尝试从中获取新冠病毒-19 JSON数据。欧洲PMC服务器返回的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)

我建议你简单地做一下

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)

这里有很多东西没有加在一起。首先;你确定你真的需要那个查询字符串吗?API不能以更有效的方式访问吗?其次,该查询字符串是您发送到服务器的内容,而不是您需要处理的内容。第三,你没有展示你试图解析到字典中的答案,我只是用一个较短的查询字符串编辑了这个问题,还提供了一个我希望格式化的结果链接。这里有很多东西没有加起来。首先;你确定你真的需要那个查询字符串吗?API不能以更有效的方式访问吗?其次,该查询字符串是您发送到服务器的内容,而不是您需要处理的内容。第三,你还没有展示你试图解析到字典中的答案。我只是用一个较短的查询字符串编辑了这个问题,还提供了一个我希望格式化的结果链接。请进一步解释@davidteather我实际上已经找到了你的答案。谢谢。请进一步解释@davidteather。我已经找到了你的答案。非常感谢。