2016-11-10 118 views
1

我想要做的是基本上從我從列表中獲得的數據寫一個新的excel文件。列表的內容是我嘗試使用xlsxwriter(特別是xlsx,因爲我使用xlsx)在新的excel文件中編寫的行內容。假設我有下面的代碼片段,它給我帶來了錯誤:Python - TypeError:'Cell'對象不可迭代

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

回答

2

的問題是,write_row取值的列表(或其他可迭代),和你傳遞一個Cell對象(cell_value)來代替。

要麼你想使用sheet.write(row_index, col_index, cell_value),或跳過內for循環和使用sheet.write_row(row_index, 0, row)

+0

事實上!謝謝!我只是有另一個錯誤,但我會張貼在一個單獨的問題,我想:) – Reiion