时间:2020-07-10 python教程 查看: 1117
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 json
import 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:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持python博客。