Python:如何使用xlwt将复数写入excel?
我正在尝试使用Python:如何使用xlwt将复数写入excel?,python,excel,complex-numbers,Python,Excel,Complex Numbers,我正在尝试使用xlwt库将Python列表写入excel文件 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 = "
xlwt
库将Python列表写入excel文件
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文件时,会出现以下错误:
Exception: Unexpected data type <type 'complex'>
异常:意外的数据类型
据我所知,write
不支持复数。有人知道如何将复杂值写入excel吗?!
另外,我不想将数据写入CSV文件。它需要是一个.xls文件。您可以使用sheet1.write(i,j,str(newdata[i][j])将复数转换为字符串。
这将帮助您摆脱回溯。尝试复数字符串表示法
str(complexnum)
谢谢@srinivasredythypathy!这很有帮助。