python将数组写入excel文件

Python022

python将数组写入excel文件,第1张

# 将数据写入新文件

def data_write(file_path, datas):

f = xlwt.Workbook()

sheet1 = f.add_sheet(u'sheet1',cell_overwrite_ok=True) #创建sheet

#将数据写入第 i 行,第 j 列

i = 0

for data in datas:

for j in range(len(data)):

sheet1.write(i,j,data[j])

i = i + 1

f.save(file_path) #保存文件

def writeToTxt(list_name,file_path):

try:

fp = open(file_path,"w+")

for item in list_name:

fp.write(str(item)+"\n")//list中一项占一行

fp.close()

except IOError:

print("fail to open file")

if __name__ == "__main__":

list_name = [ 3.00008000 +0.j,-10.58085662-19.4778165j,5.87334700 +4.733817j, -0.86048738 -0.5688545j,17.35029000 +0.j,-0.86048738 +0.5688545j,5.87334700 -4.733817j,-10.58085662+19.4778165j] //你的list

file_path = r"hello.txt"

writeToTxt(list_name,file_path)

fout = open('output.txt', 'w')  # 打开输出文件

...

print >>fout, what_you_want_to_print  # 所有的print语句都如此修改

...

fout.close()  # 最后关闭文件

这是python2.7中基本的写文件操作。