简体   繁体   English

xlsxwriter和xlwt:将字符串列表写入单元格

[英]xlsxwriter and xlwt: Writing a list of strings to a cell

I am currently using xlwt quite successfully to create .xls files. 我目前正在使用xlwt非常成功地创建.xls文件。 I am also learning xlsxwriter for possible future applications where I'll need some of its features. 我也在学习xlsxwriter以寻找可能的未来应用程序,我需要它的一些功能。

xlwt smoothly writes lists of strings into cells. xlwt平滑地将字符串列表写入单元格。

eg 例如

import xlwt
a = ['January\n','February\n','March\n','April\n','May\n','June\n']
book = xlwt.Workbook()
sheet = book.add_sheet('Test')
sheet.write(0,0,a)
book.save('Test.xls')

Open the Test.xls file, enable wrap text, and cell A1 shows: 打开Test.xls文件,启用换行文本,单元格A1显示:

January
February
March
April
May
June

I tried to do something similar with xlsxwriter 我尝试用xlsxwriter做类似的事情

import xlsxwriter
xbook = xlsxwriter.Workbook('Test.xlsx')
xsheet = xbook.add_worksheet('Test')
xsheet.write(0,0,a)

Here, I get a lengthy error message culminating in 在这里,我得到一个冗长的错误信息,最终到达

...anaconda/lib/python2.7/site-packages/xlsxwriter/worksheet.pyc in write(self, row, col, *args)
    416         # We haven't matched a supported type. Try float.
    417         try:
--> 418             f = float(token)
    419             if not self._isnan(f) and not self._isinf(f):
    420                 return self.write_number(row, col, f, *args[1:])

TypeError: float() argument must be a string or a number

I've tried other xlsxwriter write methods and all give roughly similar errors. 我尝试过其他xlsxwriter写入方法,并且都给出了大致相似的错误。

Other research: I've searched this site fairly thoroughly. 其他研究:我已经相当彻底地搜索了这个网站。 I've also gone through the excellent xlsxwriter PDF and checked the xlsxwriter Github pages. 我还浏览了优秀的xlsxwriter PDF并检查了xlsxwriter Github页面。 So far, I have not come across anything that addresses this. 到目前为止,我没有遇到任何解决这个问题的事情。

Again, xlwt is fine for now I expect to need to add charts and sparklines as well as to create xlsx files in the near future. 再次,xlwt现在很好,我希望在不久的将来添加图表和迷你图以及创建xlsx文件。

Thanks, 谢谢,

The Old Guy In The Club 俱乐部里的老家伙

No, you cannot write a list type using xlsxwrite.write() . 不,您不能使用xlsxwrite.write()编写list类型。

Check the docs 检查文档

If none of the above types are matched the value is evaluated with float() to see if it corresponds to a user defined float type. 如果上述类型都不匹配,则使用float()计算该值,以查看它是否与用户定义的float类型相对应。 If it does then it is written using write_number(). 如果是,则使用write_number()编写。

That is why you get the error TypeError: float() argument must be a string or a number 这就是你得到错误TypeError: float() argument must be a string or a number

To fix this try converting the list to a string , as suggested by Paulo Scardine (in the comments): 要解决此问题,请尝试将list转换为string ,如Paulo Scardine(在评论中)所示:

import xlsxwriter
a = ['January\n','February\n','March\n','April\n','May\n','June\n']
xbook = xlsxwriter.Workbook('Test.xlsx')
xsheet = xbook.add_worksheet('Test')
xsheet.write(0,0,''.join(a))

The write() method in XlsxWriter doesn't support lists. XlsxWriter中的write()方法不支持列表。

You can write lists using the worksheet write_row() method: 您可以使用工作表write_row()方法编写列表:

import xlsxwriter

a = ['January\n','February\n','March\n','April\n','May\n','June\n']

xbook = xlsxwriter.Workbook('Test.xlsx')
xsheet = xbook.add_worksheet('Test')

xsheet.write_row(0, 0, a)

xbook.close()

PS That exception isn't very helpful in this case and will be fixed in the next release. PS该异常在这种情况下不是很有用,将在下一个版本中修复。 Here is the exception from your example program using the version of XlsxWriter on GitHub: 以下是使用GitHub上的XlsxWriter版本的示例程序的例外:

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

I've been working through a similar problem. 我一直在解决类似的问题。

Was able to achieve the desired result by using enumerate to get the index of each string in the list and then passing that as both the Row reference and the data item to .write: 能够通过使用enumerate获取列表中每个字符串的索引,然后将其作为Row引用和数据项传递给.write来实现所需的结果:

import xlsxwriter

a = ['January\n','February\n','March\n','April\n','May\n','June\n']

xbook = xlsxwriter.Workbook('Test.xlsx')
xsheet = xbook.add_worksheet('Test')

for idx, month in enumerate(a):
    xsheet.write(idx,0,a[idx])

xbook.close()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM