Python - TypeError:"Cell"对象不可迭代



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

TypeError: 'Cell' object is not iterable

整个堆栈跟踪在写事件期间指出这一点。

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是否是原因。我试着把它打印出来,结果如下:

font: family:宋体

问题是write_row接受值的列表(或其他可迭代对象),而您传递的是单个Cell对象(cell_value)。

您要么想使用sheet.write(row_index, col_index, cell_value),要么跳过内部的for循环并使用sheet.write_row(row_index, 0, row)

最新更新