基于python实现把json数据转换成Excel表格

时间:2021-05-22

json数据:

[{"authenticate":-99,"last_ip":"156.2.98.429","last_time":"2020/05/23 01:41:36","member_id":5067002,"mg_id":1,"name":"yuanfang","status":0,"username":"L7898"},{"authenticate":-99,"last_ip":"156.2.98.421","last_time":"2020/05/20 14:16:02","member_id":1012304,"mg_id":1,"name":"管理员","status":0,"username":"aaaap"},{"authenticate":-99,"last_ip":"134.212.156.178","last_time":"2020/04/20 14:16:03","member_id":1012305,"mg_id":1,"name":"lily","status":0,"username":"aaaaa8"}]

将json数据转化为Excel表格

首先在cmd里安装tablib模块,tablib 模块可以完美实现,json 转 excel,安装方法:pip instal tablib

#!/usr/bin/python3#防止出现中文乱码# -*- coding:utf-8 -*-import jsonimport tablib# json.text文件的格式: [{"a":1},{"a":2},{"a":3},{"a":4},{"a":5}]# 获取json数据with open('F:\\阅维科技2020.4\\python测试数据\\json1.txt', 'r',encoding='utf-8',errors='ignore') as f: rows = json.load(f)# 将json中的key作为header, 也可以自定义header(列名)header=tuple([ i for i in rows[0].keys()])data = []# 循环里面的字典,将value作为数据写入进去for row in rows: body = [] for v in row.values(): body.append(v) data.append(tuple(body))#将含标题和内容的数据放到data里data = tablib.Dataset(*data,headers=header)#写到桌面open('C:\\Users\\91911\\Desktop\\data.xls', 'wb').write(data.xls)

结果放到了桌面,名为data.xls:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章