Python – TypeError:'Cell'对象不可迭代

我想要做的是基本上从我从列表中获得的数据写一个新的Excel文件。 列表的内容是我试图写在使用xlsxwriter(特别是xlsx,因为我使用xlsx)的新的Excel文件中的行内容。 假设我有下面的代码片段,它给我的错误:

TypeError:“单元格”对象不可迭代

整个堆栈跟踪在写入事件中指出了这一点。

Traceback (most recent call last): File "Desktop/excel-copy.py", line 33, in <module> sheet.write_row(row_index, col_index, cell_value) File "/usr/local/lib/python2.7/dist-packages/xlsxwriter/worksheet.py", line 64, in cell_wrapper return method(self, *args, **kwargs) File "/usr/local/lib/python2.7/dist-packages/xlsxwriter/worksheet.py", line 989, in write_row for token in data: TypeError: 'Cell' object is not iterable import xlrd import xlsxwriter new_workbook = xlsxwriter.Workbook() sheet = new_workbook.add_worksheet('stops') #copy all row and column contents to new worksheet for row_index, row in enumerate(ordered_list_stops): for col_index, cell_value in enumerate(row): print("WRITING: " + str(cell_value) + "AT " + str(row_index)+ " " + str(col_index)) sheet.write_row(row_index, col_index, cell_value) new_workbook.save('output.xlsx') 

我无法指出cell_value是否是原因。 我试图打印出来,这是结果:

写作:文本:u'4977'AT 0 0

问题是, write_row需要一个列表(或其他可迭代的)值,而你正在传递一个Cell对象( cell_value )。

你要么使用sheet.write(row_index, col_index, cell_value) ,要么跳过inner for循环,并使用sheet.write_row(row_index, 0, row)