直接上代码:
# -*- coding: utf-8 -*-
import xlsxwriter as xw
def xw_toExcel(data, fileName): # xlsxwriter库储存数据到excel
workbook = xw.Workbook(fileName) # 创建工作簿
worksheet1 = workbook.add_worksheet("sheet1") # 创建子表
worksheet1.activate() # 激活表
title = ['序号', '预测值', '真实值'] # 设置表头
worksheet1.write_row('A1', title) # 从A1单元格开始写入表头
i = 2 # 从第二行开始写入数据
for j in range(len(data)):
insertData = [data[j]["id"], data[j]["预测值"], data[j]["真实值"]]
row = 'A' + str(i)
worksheet1.write_row(row, insertData)
i += 1
workbook.close() # 关闭表
# "-------------数据用例-------------"
testData = [
{"id":i, "预测值":y[i-1][0] , "真实值":y[i-1][1]} for i in range(1, 621)
]
fileName = '测试.xlsx'
xw_toExcel(testData, fileName)