json转excle代码
  DI649WFdAlDI 2023年11月02日 127 0

一、需要安装pandas

二、我的json文件内容

[{"name": "John", "age": 28, "city": "New York"},{"name": "Alice", "age": 24, "city": "San Francisco"},{"name": "Bob", "age": 32, "city": "Chicago"}]

将以上内容复制到记事本里,然后重命名为example.json即可。 

三、源码如下:

import json  

import pandas as pd  


def read_json_file(file_path):  

  with open(file_path, 'r', encoding='utf-8') as file:  

      data = json.load(file)  

  return data

def json_to_excel(json_data, excel_file):  

   # 将JSON数据加载为pandas的DataFrame  

   data = pd.json_normalize(json_data)  

 

   # 将DataFrame写入Excel文件  

   data.to_excel(excel_file, index=False)  

 

if __name__ == '__main__':  

   # JSON数据  

   # 这里的 JSON 文件路径仅作示例,请替换成你需要的 JSON 文件路径  

   json_file_path = "D:\code\example.json"


  # 读取 JSON 文件  

   json_data = read_json_file(json_file_path)

 

   # 输出Excel文件名  

   excel_file = 'output1.xlsx'  

 

   # 将JSON数据转换为Excel  

   json_to_excel(json_data, excel_file)  

 

   print(f"JSON数据已成功转换为Excel文件:{excel_file}")

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
  anLrwkgbyYZS   2023年12月30日   28   0   0 i++iosi++ioscici
  anLrwkgbyYZS   2023年12月30日   33   0   0 ideciciMaxideMax
DI649WFdAlDI