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

python python-3.x xlsxwriter

3769 просмотра

1 ответ

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()
Автор: mickeymouse Источник Размещён: 08.11.2019 10:56

Ответы (1)


0 плюса

Решение

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
Автор: jmcnamara Размещён: 21.08.2016 12:22
Вопросы из категории :
32x32