我們在做大數據測試分析的時候,經常需要用到excel 統計分析數據,以下例子就是
python 調用xlwt 寫excel 文件:
import sys
import random
import time
import xlwt
import codecs
def Txt_to_Excel(ws,inputTxt,start_row,start_col):
fr = codecs.open(inputTxt,'r')
line_number = 0
row_excel = start_row
for line in fr:
#print(line)
line_number +=1
row_excel +=1
line =line.strip()
#print(line)
line =line.split()
#print(line)
b =line[3:4]
#print(b)
col_excel =start_col;
ws.write(row_excel,col_excel,b)
if __name__ == '__main__':
outputfile ='excel_result1.xls'
sheet_name1='rnnoise'
sheet_name2='nsdk'
wb = xlwt.Workbook(encoding = 'utf-8')
ws1 = wb.add_sheet(sheet_name1)
ws2 = wb.add_sheet(sheet_name2)
startrow =0
dir1 = './0db/pesq_results1.txt'
startcol1 =0
Txt_to_Excel(ws1,dir1,startrow,startcol1)