Python:如何使用xlwt编写一个复杂的数字到excel? [英] Python: How to write a complex number to excel using xlwt?
问题描述
我正在使用 xlwt
库将一个Python列表写入excel文件。
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())
它适用于常见的变量类型(例如int,float,string),但是当我尝试将一个复数写入excel文件,我收到以下错误:
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'>
据了解,写入
不支持复杂数字。有没有人知道如何写复杂的价值观excel?
P.S.我不想将数据写入CSV文件。它需要是一个.xls文件。
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.
推荐答案
您可以使用sheet1.write将复数转换成字符串(我, Ĵ,STR(newdata [i] [j]))。
这将帮助您摆脱追踪。
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.
这篇关于Python:如何使用xlwt编写一个复杂的数字到excel?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!