Python 数据保存到xls文件
时间: 2023-11-05 22:56:39 浏览: 88
你可以使用xlwt库来将Python数据保存到xls文件中。下面是一个示例代码,演示了如何使用xlwt库来保存数据到xls文件中:
```
import xlwt
def saveData(datalist, savepath):
book = xlwt.Workbook(encoding="utf-8", style_compression=0)
sheet = book.add_sheet('test', cell_overwrite_ok=True)
col = ["学号", "姓名", "班级", "专业"]
for i in range(0, len(col)):
sheet.write(0, i, col[i])
for i in range(len(datalist)):
data = datalist[i]
for j in range(0, len(col)):
sheet.write(i+1, j, data[j])
book.save(savepath)
if __name__ == "__main__":
datalist = [["001", "小明", "1班", "软件工程"], ["002", "小红", "1班", "软件工程"], ["003", "小东", "2班", "网络工程"], ["004", "小华", "2班", "网络工程"], ["005", "小芳", "3班", "计算机科学与技术"], ["006", "小莉", "3班", "计算机科学与技术"]]
savepath = "data.xls"
saveData(datalist, savepath)
```
你可以根据需要修改datalist和savepath的值来保存不同的数据和文件名。