基于python实现把json数据转换成Excel表格
json数据:
[{"authenticate":-99,"last_ip":"156.2.98.429","last_time":"2020/05/2301: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/2014: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/2014:16:03","member_id":1012305,"mg_id":1,"name":"lily","status":0,"username":"aaaaa8"}]
将json数据转化为Excel表格
首先在cmd里安装tablib模块,tablib模块可以完美实现,json转excel,安装方法:pipinstaltablib
#!/usr/bin/python3 #防止出现中文乱码 #-*-coding:utf-8-*- importjson importtablib #json.text文件的格式:[{"a":1},{"a":2},{"a":3},{"a":4},{"a":5}] #获取json数据 withopen('F:\\阅维科技2020.4\\python测试数据\\json1.txt','r',encoding='utf-8',errors='ignore')asf: rows=json.load(f) #将json中的key作为header,也可以自定义header(列名) header=tuple([iforiinrows[0].keys()]) data=[] #循环里面的字典,将value作为数据写入进去 forrowinrows: body=[] forvinrow.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:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。