"TypeError: 不支持的类型 <class 'list'>在写()" [英] "TypeError: Unsupported type <class 'list'> in write()"

查看:43
本文介绍了"TypeError: 不支持的类型 <class 'list'>在写()"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当条件不是大写时,我希望在 excel 文件中打印 'out.csv' 数据.但是 out.csv 中的数据是数据列表而不是字符串.如何将列表写入excel文件而不将其转换为字符串?(因为我有其他文件可能需要使用列表而不是字符串)

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 版本 #3.5.1

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()

推荐答案

如何将列表写入excel文件而不将其转换为字符串

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

您可以遍历列表并write() 输出每个元素,或者您可以使用 XlsxWriter write_row() 一次性写入列表的方法.

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.

像这样:

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

这篇关于"TypeError: 不支持的类型 <class 'list'>在写()"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆