【问题标题】:How to select data from JSON to Excel with Pandas如何使用 Pandas 从 JSON 中选择数据到 Excel
【发布时间】:2021-08-12 05:56:03
【问题描述】:

晚上好!

我正在处理一个项目,我想从网站中提取 JSON 数据,然后将其导入 Excel/CSV 文件。我正在使用 Selenium 抓取网页,并使用 JSON 到 json_loads 和 json_normalize。当我使用 json_normalize 函数时,打印出来时不会出现所有数据。我想做的是选择一些数据,让它看起来不错。

来自网站的 JSON 数据:

{
  "getUrl": "/395012/Organization/pase10001",
  "className": "Organization",
  "data": {
    "name": "ICA Supermarket",
    "organizationNumber": "556589-4341",
    "centralPhoneNumber": {
      "value": "044-310010",
      "normalized": "+4644310010",
      "className": "PhoneNumber",
      "isEmpty": false
    },
    "faxPhoneNumber": {
      "value": null,
      "normalized": null,
      "className": "PhoneNumber",
      "isEmpty": true
    },
    "website": "www.ica.se",
    "email": {
      "value": "kundkontakt.tollarp@supermarket.ica.se",
      "className": "Email"
    },
    "dateLastModified": "/Date(1621342946134+0200)/",
    "visitAddress": {
      "street": "Polgatan 5",
      "zipCode": "298 32",
      "city": "TOLLARP",
      "countryCode": ""
    },
    "postalAddress": {
      "street": "Box 24",
      "zipCode": "298 21",
      "city": "TOLLARP",
      "countryCode": ""
    },
    "responsibleCoworker": null,
    "integrationid": "",
    "customFields": [],
    "relation": 0,
    "tags": [],
    "headOffice": null,
    "corporateGroup": null,
    "sharedBody": {
      "vatNumber": "SE556589434101",
      "lineOfBusiness": "Livsmedelshandel med brett sortiment, ej varuhus eller stormarknad",
      "businessDescription": "Bolaget skall som medlem i ICA-förbundet bedriva detaljhandelsrörelse med dagligvaror och annan därmed förenlig verksamhet.",
      "legalForm": "Aktiebolag",
      "dateOfRegistration": "2000-04-06",
      "legalName": "Superlivs i Tollarp AB",
      "rating": null,
      "numberOfSubsidaries": 0,
      "numberOfEmployeesRange": "20 - 49",
      "numberOfEmployeesWorkSite": "20 - 49"

我想选择此 JSON 数据的某些信息并将其导出到 Excel 文档。我希望 Excel 具有此顺序的类别,例如:

Company organization
name1 000000000000

到目前为止我所做的尝试:

res = self.driver.find_element_by_tag_name("pre").text
        data = json.loads(res)

        xd = pd.json_normalize(data])
        xd.to_excel("output.xlsx")

Screenshot of Excel document

我是 Python 新手,并尝试尽可能多地学习。如果你能帮助我如何在这个项目中进一步发展,那真的会让我很开心!

【问题讨论】:

  • 您的方法看起来是正确的。示例 JSON 格式不正确,但通过添加数字或大括号来修复。这给了我 41 列。显然这些可以重命名,您可以选择一个子集作为to_csv()to_excel() 的输入
  • @RobRaymond 添加数字是什么意思?
  • 您的示例 json 缺少三个(数字)大括号 }}}

标签: python json excel pandas selenium


【解决方案1】:

您也可以尝试convtools 库,这将帮助您动态构建转换器。 备忘单是here

import json
from convtools import conversion as c

data = json.loads(res)
converter = c.list_comp({
    "company": c.item("data", "name"),
    "org_number": c.item("data", "organizationNumber"),
}).gen_converter(debug=True)  # install "black" to see formatted sources

prepared_data = converter([json.loads(res)])

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-07
    • 1970-01-01
    • 2019-12-02
    • 2018-02-06
    • 2019-04-08
    • 2012-07-28
    • 2019-03-05
    • 1970-01-01
    相关资源
    最近更新 更多