码迷,mamicode.com
首页 > 编程语言 > 详细

python将json数据转化为Excel表格

时间:2020-05-04 17:41:47      阅读:131      评论:0      收藏:0      [点我收藏+]

标签:bin   自定义   ica   安装   安装方法   实现   出现   文件   xls   

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将json数据转化为Excel表格

标签:bin   自定义   ica   安装   安装方法   实现   出现   文件   xls   

原文地址:https://www.cnblogs.com/xiao02fang/p/12826899.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!