Python-类型错误 : 'Cell' object is not iterable

标签 python excel xlsxwriter

我想做的基本上是根据从列表中获得的数据编写一个新的 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 是否是原因。我尝试将其打印出来,结果如下:

WRITING: text:u'4977'AT 0 0

最佳答案

问题在于 write_row 接受值的列表(或其他可迭代的),并且您传递单个 Cell 对象(cell_value) 相反。

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

关于Python-类型错误 : 'Cell' object is not iterable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40521518/

相关文章:

python - 在 Apache 上运行时加载 pyodbc 模块时出错

javascript - 将数据从表单插入MySQL数据库、javascript和flask

python - Pygame 游戏加载时卡住

excel - 使用 Excel VBA : run-time error 1004 在工作簿中查找匹配项

excel - 检测是否安装了 Excel 2003 的最佳方法是什么?

python - 使用 xlsxwriter 在条件格式(公式)后删除列

python - 如何在Python中将多个字符变成一个字符?

python - 如何使用openpyxl同时在一个工作簿中写两张纸

python - 在 excel xlsxwriter 中追加行

python - 如何在xlsxwriter中自动设置列宽