首页 > 编程语言 > 详细

json数组字符串写入excel

时间:2021-01-22 19:34:29      阅读:50      评论:0      收藏:0      [点我收藏+]
#!/usr/bin/python
# -*- coding:utf-8 -*-

import json
import xlrd
import xlwt
from xlutils import copy

str_json = ‘{"1":["小花",99,100,98.5],"2":["小王",90,30.5,95],"3":["小明",67.5,49.6,88]}‘
dic = json.loads(str_json)
# print(dic)
value_list = []
for num, value in dic.items():
# print(num,value)
value.insert(0,num)
value_list.append(value)
# print(value_list)
#写excel
book =xlwt.Workbook()
sheet = book.add_sheet(‘student‘) #sheet=student
title = [‘编号‘,‘姓名‘,‘语文成绩‘,‘数学成绩‘,‘英语成绩‘,‘总分‘,‘平均分‘]
for i in range(len(title)): #写title
sheet.write(0,i,title[i])
# book.save(‘student1.xls‘) #保存,文件名

for i in range(len(value_list)):
for y in range(len(value_list[i])):
sheet.write(i+1,y,value_list[i][y])
book.save(‘student1.xls‘) #,保存,文件名,写语数外

#读excel
book = xlrd.open_workbook(‘student1.xls‘)
sheet = book.sheet_by_index(0)
# print(sheet.ncols)
# print(sheet.nrows)
#写
book = xlrd.open_workbook(‘student1.xls‘)
new_book = copy.copy(book)
new_sheet = new_book.get_sheet(‘student‘)
# new_sheet.write(4,8,‘daodao‘)
# new_book.save(‘student1.xls‘) 可以写入

for i in range(1, sheet.nrows):
# print(i)
row_list= sheet.row_values(i)[2:5] #取所有列成绩
print(row_list)
toall_score = round(sum(row_list), 2) #单列成绩总数
avg_score = round((toall_score/len(row_list)), 2)
new_sheet.write(i,5,toall_score)
new_sheet.write(i,6,avg_score)
new_book.save(‘my_student.xls‘)

json数组字符串写入excel

原文:https://www.cnblogs.com/wsc001/p/14314892.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!