问题
I am trying to write a Python list to an excel file using xlwt
library.
import xlwt
from tempfile import TemporaryFile
book = xlwt.Workbook()
sheet1 = book.add_sheet('sheet1')
for i in range(len(newdata)):
for j in range(len(newdata[i])):
sheet1.write(i,j,newdata[i][j])
name = "my_file.xls"
book.save(name)
book.save(TemporaryFile())
It work for common variable types (e.g. int, float, string) but when I try to write a complex number to the excel file, I get the following error:
Exception: Unexpected data type <type 'complex'>
As I understand write
does not support complex numbers. Does anyone know how to write complex values to excel?!
P.S. I don't want to write the data to a CSV file. It needs to be a .xls file.
回答1:
You can convert the complex number into a string using sheet1.write(i,j,str(newdata[i][j])). This will help you get out of the traceback.
来源:https://stackoverflow.com/questions/19109281/python-how-to-write-a-complex-number-to-excel-using-xlwt