This article mainly introduces the Python implementation of reading json files into excel tables. It has certain reference value. Interested friends can refer to it.
The example in this article shares the Python implementation of reading. json file to the excel table for your reference. The specific content is as follows
1. Requirements
1. 'score.json' file content:
{ "1":["小花",99,100,98.5], "2":["小王",90,30.5,95], "3":["小明",67.5,49.6,88] }
2. Read the json file and save it to the database, and calculate the total score and average score of each person
2. Implementation code
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')
Related recommendations:
Python implements reading strings and distributing them in columns and outputting them in rows
The above is the detailed content of Python implements reading json files into excel tables. For more information, please follow other related articles on the PHP Chinese website!