“TypeError: Unsupported type <class 'list'> in write()”

 ̄綄美尐妖づ 提交于 2019-12-23 17:18:04

问题


I wish to print 'out.csv' data in excel file when the condition is not uppercase. But the data in out.csv is list of data instead of string. How do I write the list to excel file without converting it to string? (As I have other file which may need to use list instead of string)

Python version #3.5.1

import xlsxwriter
import csv
import xlwt

f1= open('out.csv')
data=csv.reader(f1)

# Create a new workbook and add a worksheet
workbook = xlsxwriter.Workbook('1.xlsx')
worksheet = workbook.add_worksheet()

# Write some test data.

for module in data:
    str1 = ''.join(module)
    if str1.isupper():
      pass
    else:
      worksheet.write('A', module)


workbook.close()

回答1:


How do I write the list to excel file without converting it to string

You could either loop over the list and write() out each element or you could use the XlsxWriter write_row() method to write the list in one go.

Something like this:

row = 0
col = 0
for module in data:
    str1 = ''.join(module)
    if str1.isupper():
        pass
    else:
        worksheet.write_row(row, col, module)
        row += 1


来源:https://stackoverflow.com/questions/39050951/typeerror-unsupported-type-class-list-in-write

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!