gpt4 book ai didi

python - 如何使用多表将 JSON 转换为 excel 文件?

转载 作者:太空宇宙 更新时间:2023-11-03 14:21:43 34 4
gpt4 key购买 nike

我有一组 300 多个 JOSN 文件,例如 location_X.json、location_Y.json 等,其结构类似于我的文件 location_X.json

 {"Cardiologist": [{"name": "Dr. AB", "url": "https://www....-cardiologist?specialization=Cardiologist", "photo": [], "image": "https://images1-....jpg/thumbnail", "address": {"addressLocality": "location_X", "addressRegion": "delhi", "@type": "PostalAddress"}, "branchOf": {"url": "https://www....-clinic-x-y-market", "address": {"addressLocality": "X.Y.Market", "addressRegion": "delhi", "@type": "PostalAddress"}, "@type": "MedicalClinic", "name": "Dr AB Clinic"}, "priceRange": 0, "openingHours": [], "currenciesAccepted": "INR", "@context": "http://schema.org/", "geo": {"latitude": "", "@type": "GeoCoordinates", "longitude": ""}, "@type": "Physician", "medicalSpecialty": "Cardiologist"}], ... }

你可以这样想结构:

{"Specialty_1":[{"name":"","url":"", "photo":"", "address":{"addressLocality": "", "addressRegion": "", "@type": "PostalAddress"},"branchOf": {"url": "https://www...", "address": {"addressLocality": "", "addressRegion": "", "@type": "PostalAddress"},"@type": "MedicalClinic", "name": "Dr AB Clinic"}, "priceRange": 0, "openingHours": [], "currenciesAccepted": "", "@context": "http://schema.org/", "geo": {"latitude": "", "@type": "GeoCoordinates", "longitude": ""}, "@type": "Physician", "medicalSpecialty": "Specialty_1"}, ...], "Specialty_2":[{"name":"","photo":"", ...}, ...], ... }

包含特定领域所有特定专业医生列表的字典,具有 13 种特征,并由该领域的所有医生组成,

如何转换此 JSON 文件,以便在 Excel 中获得一个带有多表的 location_X 文件,每个表的名称类似于“Specialty_1”、“Specialty_2”等...包含“Specialty_1”等的所有医生位置 X。提前致谢。

最佳答案

你可以尝试一下 Pandas。其文档:https://pandas.pydata.org/

基本上,你应该做的是:

import pandas as pd
import json
json_data = json.load(open('<your_json_file>'))
data = pd.read_json(json_data)
excel_file = pd.ExcelWriter('output.xlsx')
data.to_excel(writer, 'sheet1')
writer.save()

上面的代码将 json 化的数据保存到 output.xlsx 中名为“sheet1”的工作表中。如果你想将数据保存在不同的sheet中,你可以先对你的数据进行处理,将它们分成不同的json化数据。然后调用to_excell函数将它们分别保存到不同的sheet中。

关于python - 如何使用多表将 JSON 转换为 excel 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47898531/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com