你怎么写几个数组到一个Excel文件?

如果我有几个数组,我想用Python写入一个excel文件,那么最好的办法是什么? 我已经尝试了几种方法,无法弄清楚….这是我正在尝试的一种方式的一个例子…我对此很新

import xlwt from tempfile import TemporaryFile book = xlwt.Workbook() sheet1 = book.add_sheet('sheet1') a=[1,2,3,4,5] b=[6,7,8,9,10] c=[2,3,4,5,6] data = [a,b,c] for i,e in enumerate(data): sheet1.write(i,1,e) name = "this.xls" book.save(name) book.save(TemporaryFile()) 

根据Steven Rumbalski的build议,

 import xlwt from tempfile import TemporaryFile book = xlwt.Workbook() sheet1 = book.add_sheet('sheet1') a=[1,2,3,4,5] b=[6,7,8,9,10] c=[2,3,4,5,6] data = [a,b,c] for row, array in enumerate(data): for col, value in enumerate(array): sheet1.write(row, col, value): name = "this.xls" book.save(name) book.save(TemporaryFile()) 

另一种select是将数组写为分隔文本文件。 Excel可以轻松地阅读这些(只要打开它们,就像他们是一个Excel工作表,你会得到导入对话框)。

这是做这个的代码 –

 path='foo.txt' a=[1,2,3,4,5] b=[6,7,8,9,10] c=[2,3,4,5,6] with open(path,'w') as table: for row in zip(a,b,c): for cell in row: table.write(str(cell) + '\t') table.write('\n') 

在这种情况下,数组是垂直写入,单元格由选项卡分隔(Excel处理冗余选项卡没有问题)。