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