【问题标题】:Parsing a dictionary into many values and making a table in python将字典解析为多个值并在 python 中制作表格
【发布时间】:2016-12-06 16:25:07
【问题描述】:

我正在尝试使用 Scopus API 来检索由 scopus 分配给作者的唯一作者标识符号。在某些情况下,一个名字会产生多个作者 ID。我的麻烦在于读取 json 格式,以便我可以解析出 'dc:identifiers'

这是我的代码:

    import requests
    import json

    url = "http://api.elsevier.com/content/search/author"

    querystring = {"apiKey":"xxxxxxxxxxxxx","query":["AUTHLASTNAME(Volaire)","AUTHFIRST(F)"]}

    headers = {
   'cache-control': "no-cache",
   'postman-token': "5c0df5ad-2645-8e88-0f8a-10795757b4ec"
    }

   response = requests.request("GET", url, headers=headers, params=querystring)

   results = response.json()

然后我得到以下输出:

{
  "search-results": {
    "opensearch:totalResults": "3",
    "opensearch:startIndex": "0",
    "opensearch:itemsPerPage": "3",
    "opensearch:Query": {
      "@role": "request",
      "@searchTerms": "AUTHLASTNAME(Volaire)",
      "@startPage": "0"
    },
    "link": [
      {
        "@_fa": "true",
        "@href": "http://api.elsevier.com/content/search/author?start=0&count=25&query=AUTHLASTNAME%28Volaire%29&apiKey=4275854a6db51fb373ef69fb1337ccf9",
        "@ref": "self",
        "@type": "application/json"
      },
      {
        "@_fa": "true",
        "@href": "http://api.elsevier.com/content/search/author?start=0&count=25&query=AUTHLASTNAME%28Volaire%29&apiKey=4275854a6db51fb373ef69fb1337ccf9",
        "@ref": "first",
        "@type": "application/json"
      }
    ],
    "entry": [
      {
        "@_fa": "true",
        "link": [
          {
            "@_fa": "true",
            "@href": "http://api.elsevier.com/content/author/author_id/55910750900",
            "@ref": "self"
          },
          {
            "@_fa": "true",
            "@href": "http://api.elsevier.com/content/search/author?query=au-id%2855910750900%29",
            "@ref": "search"
          },
          {
            "@_fa": "true",
            "@href": "https://www.scopus.com/author/citedby.uri?partnerID=HzOxMe3b&citedAuthorId=55910750900&origin=inward",
            "@ref": "scopus-citedby"
          },
          {
            "@_fa": "true",
            "@href": "https://www.scopus.com/authid/detail.uri?partnerID=HzOxMe3b&authorId=55910750900&origin=inward",
            "@ref": "scopus-author"
          }
        ],
        "prism:url": "http://api.elsevier.com/content/author/author_id/55910750900",
        "dc:identifier": "AUTHOR_ID:55910750900",
        "eid": "9-s2.0-55910750900",
        "preferred-name": {
          "surname": "Volaire",
          "given-name": "Florence A.",
          "initials": "F.A."
        },
        "name-variant": [
          {
            "@_fa": "true",
            "surname": "Volaire",
            "given-name": "F.",
            "initials": "F."
          },
          {
            "@_fa": "true",
            "surname": "Volaire",
            "given-name": "Florence",
            "initials": "F."
          }
        ],
        "document-count": "41",
        "subject-area": [
          {
            "@abbrev": "ENVI",
            "@frequency": "7",
            "$": "Environmental Science (all)"
          },
          {
            "@abbrev": "AGRI",
            "@frequency": "50",
            "$": "Agricultural and Biological Sciences (all)"
          },
          {
            "@abbrev": "ENGI",
            "@frequency": "4",
            "$": "Engineering (all)"
          }
        ],
        "affiliation-current": {
          "affiliation-url": "http://api.elsevier.com/content/affiliation/affiliation_id/60020315",
          "affiliation-id": "60020315",
          "affiliation-name": "INRA Institut National de La Recherche Agronomique",
          "affiliation-city": "Paris",
          "affiliation-country": "France"
        }
      },
      {
        "@_fa": "true",
        "link": [
          {
            "@_fa": "true",
            "@href": "http://api.elsevier.com/content/author/author_id/6603570012",
            "@ref": "self"
          },
          {
            "@_fa": "true",
            "@href": "http://api.elsevier.com/content/search/author?query=au-id%286603570012%29",
            "@ref": "search"
          },
          {
            "@_fa": "true",
            "@href": "https://www.scopus.com/author/citedby.uri?partnerID=HzOxMe3b&citedAuthorId=6603570012&origin=inward",
            "@ref": "scopus-citedby"
          },
          {
            "@_fa": "true",
            "@href": "https://www.scopus.com/authid/detail.uri?partnerID=HzOxMe3b&authorId=6603570012&origin=inward",
            "@ref": "scopus-author"
          }
        ],
        "prism:url": "http://api.elsevier.com/content/author/author_id/6603570012",
        "dc:identifier": "AUTHOR_ID:6603570012",
        "eid": "9-s2.0-6603570012",
        "preferred-name": {
          "surname": "Volaire",
          "given-name": "Florence",
          "initials": "F."
        },
        "name-variant": [
          {
            "@_fa": "true",
            "surname": "Volaire",
            "given-name": "F.",
            "initials": "F."
          }
        ],
        "document-count": "8",
        "subject-area": [
          {
            "@abbrev": "AGRI",
            "@frequency": "13",
            "$": "Agricultural and Biological Sciences (all)"
          },
          {
            "@abbrev": "BIOC",
            "@frequency": "1",
            "$": "Biochemistry, Genetics and Molecular Biology (all)"
          }
        ],
        "affiliation-current": {
          "affiliation-url": "http://api.elsevier.com/content/affiliation/affiliation_id/60020315",
          "affiliation-id": "60020315",
          "affiliation-name": "INRA Institut National de La Recherche Agronomique",
          "affiliation-city": "Paris",
          "affiliation-country": "France"
        }
      },
      {
        "@_fa": "true",
        "link": [
          {
            "@_fa": "true",
            "@href": "http://api.elsevier.com/content/author/author_id/24571987100",
            "@ref": "self"
          },
          {
            "@_fa": "true",
            "@href": "http://api.elsevier.com/content/search/author?query=au-id%2824571987100%29",
            "@ref": "search"
          },
          {
            "@_fa": "true",
            "@href": "https://www.scopus.com/author/citedby.uri?partnerID=HzOxMe3b&citedAuthorId=24571987100&origin=inward",
            "@ref": "scopus-citedby"
          },
          {
            "@_fa": "true",
            "@href": "https://www.scopus.com/authid/detail.uri?partnerID=HzOxMe3b&authorId=24571987100&origin=inward",
            "@ref": "scopus-author"
          }
        ],
        "prism:url": "http://api.elsevier.com/content/author/author_id/24571987100",
        "dc:identifier": "AUTHOR_ID:24571987100",
        "eid": "9-s2.0-24571987100",
        "preferred-name": {
          "surname": "Volaire",
          "given-name": "M.",
          "initials": "M."
        },
        "name-variant": [
          {
            "@_fa": "true",
            "surname": "Volaire",
            "given-name": null,
            "initials": "M."
          }
        ],
        "document-count": "1",
        "subject-area": [
          {
            "@abbrev": "CHEM",
            "@frequency": "2",
            "$": "Chemistry (all)"
          },
          {
            "@abbrev": "BIOC",
            "@frequency": "1",
            "$": "Biochemistry, Genetics and Molecular Biology (all)"
          },
          {
            "@abbrev": "ENVI",
            "@frequency": "1",
            "$": "Environmental Science (all)"
          }
  1. 我需要做的是解析这个,以便获得被拉起的“dc:identifiers”。在这种情况下,有 3 个。

  2. 理想情况下,我想把它放到我有的表中 AUTHLASTNAME / AUTHFIRST / DCIDENTIFER(列)

任何建议表示赞赏。

【问题讨论】:

  • 发布的代码与问题无关(关于处理它产生的数据);这是正确的吗?
  • @ScottHunter:没错,只是它告诉你我是如何得到 json 输出的。
  • 您如何获得数据无关紧要;并且您发布的输出不完整。

标签: python json dictionary python-requests


【解决方案1】:

这将获得第 1 部分的信息:

print( [e["dc:identifier"] for e in results['search-results']['entry']] )

您可以对来自每个entry 的其他信息使用类似的内容。

至于第 2 部分:python 没有表。

【讨论】:

    【解决方案2】:

    也许是这样的? 你在这里的一切都在轨道上:)

    print(results['search-results']['link']['entry']['dc:identifier'])
    

    试试这个。它应该可以工作,因为您想要的值位于 json 列表中的对象内。

    some_dict = [
        { 
            "dc:identifiers": r['dc:identifier'], 
            "AUTHLASTNAME":  r['preferred-name']['surname'],
            "AUTHFIRST":  r['preferred-name']['given-name'],
        } for r in results['search-results']['entry']
    ]
    

    显然这是假设您的 json 保持这种格式。 还要检查我输入的 results['search-results']['entry'] 实际上是否正确。我没有三次检查它

    【讨论】:

    • 谢谢,但我得到了这个:“语法错误:解析时意外 EOF”
    • 我猜 [search-results][entry] 是让我感到困惑的部分,因为列表中的对象太多...
    猜你喜欢
    • 2020-03-31
    • 2018-02-11
    • 1970-01-01
    • 2021-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-14
    • 1970-01-01
    相关资源
    最近更新 更多