字典列表类型数据写入excel。
#导入xlwt库
import xlwt
import os
# 步骤1:获取excel文件的绝对路径
dirPath = os.path.join(os.getcwd(),"data")
if not os.path.exists(dirPath):
os.mkdir(dirPath) excelPath = os.path.join(dirPath,"data.xls")
# 步骤2:创建工作簿 workbook workbook = xlwt.Workbook(encoding="utf-8")
# 步骤3:在工作簿中创建sheet单页
worksheet = workbook.add_sheet("数据测试单页")
# 步骤4:添加数据
#步骤4-1:设置表头数据
headers=["字段1","字段2","字段3"]
#步骤4-2:循环输入表头数据
for colsIndex in range(len(headers)):
worksheet.write(0,colsIndex,headers[colsIndex]) #步骤4-3:循环写入其他表格内容
for rowIndex in range(1,4):
for colsIndex in range(len(headers)):
worksheet.write(rowIndex,colsIndex,(rowIndex+colsIndex))
# 步骤5:保存工作簿workbook
workbook.save(excelPath)
print("excel文件写入完毕")