Python实现读取json文件到excel表


当前第2页 返回上一页

二、实现代码

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

import json, xlwt

 

 

def read_score(jsonfile):

  with open(jsonfile, encoding='utf-8') as f: # 将json文件转化为字典

    score_all = json.load(f)

 

  book = xlwt.Workbook() # 创建excel文件

  sheet = book.add_sheet('sheet1') # 创建一个表

  title = ['序号', '姓名', '语文', '数学', '英语', '总分', '平均分']

  for col in range(len(title)): # 存入第一行标题

    sheet.write(0, col, title[col])

  row = 1 # 定义行

  for k in score_all:

    data = score_all[k] # data保存姓名和分数的list

    data.append(sum(data[1:4])) # 倒数第二列加入总分

    data.append(sum(data[1:4]) / 3.0) # 最后一列加入平均分

    data.insert(0, k) # 第一列加入序号

    for index in range(len(data)): # 依次写入每一行

      sheet.write(row, index, data[index])

    row += 1

  book.save('score.xls')

 

 

read_score('score.json')

相关推荐:

Python实现读取目录所有文件的文件名并保存到txt文件代码

Python实现读取字符串按列分配后按行输出

以上就是Python实现读取json文件到excel表的详细内容,更多文章请关注木庄网络博客!!

返回前面的内容

相关阅读 >>

Python如何安装wordcloud库

Python如何发送邮件

如何查找Python的安装路径

Python步长什么意思

Python课程什么意思

会java再去学Python容易吗

Python该怎么学才快

Python拿mac还是windows?

Python学来干什么

Python有eval函数吗

更多相关阅读请进入《Python》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...