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

时间:2017-12-20 03:51:27

标签: python json excel csv xls

我有一组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.提前感谢。

1 个答案:

答案 0 :(得分:0)

你可以试试熊猫。其文档: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()

上面的代码将jsonified数据保存到名为&#39; sheet1&#39;在output.xlsx中。如果要将数据保存在不同的工作表中,可以先处理数据,然后将它们分成不同的jsonified数据。然后调用to_excell函数将它们分别保存到不同的表格中。