2017-08-16 70 views
0

设置:我使用的笔记本jupyter,Python版本3.6.2,和Excel版本15.36如何复制列表的内容脱颖而出

任务:我创建了一个列表,并希望它的内容,以复制到一列空白Excel文件的第一行。

这里是我的代码:

for rowOfCellObjects in strauss_sheet(1, list_length): 
    for cellObj in rowOfCellObjects: 
     for item in noreplist: 
      sheet.cellObj(row=1, column=colNum).value = item 

我得到一个错误,因为“工作表”对象是不可调用的。 我有我的列表长度存储在list_length,我的列表是noreplist

我是python的新手,很想听听执行此任务的好方法。

+0

你使用[openpyxl](http://pypi.python.org/pypi/openpyxl),你更喜欢使用特定的python包来做到这一点吗? – davedwards

+0

@downshift是的,我使用的是完美工作的openpyxl – user101981

回答

0

这可以很容易地与xlwt来完成:

import xlwt 

wb = xlwt.Workbook() 
ws = wb.add_sheet('Sheet 1') 

listdata = ['write', 'list', 'to', 'excel', 'row'] 

first_row = 0 

# write each item in the list to consecutive columns on the first row 
for index, item in enumerate(listdata): 
     ws.write(first_row, index, item) 

wb.save('example.xls') 

产生的excel文件,其内容:

write list to excel row

希望这有助于。

+0

!谢谢您的帮助! – user101981