import xlwt
import os
from os import path
import random
text_condition=("C1","C2","C3","C4")
bin_kind=("0",)
ply_name=("bridge_1mm","double_T_section_1mm","intersection1_1mm","intersection2_1mm","straight_road_1mm","T_section_1mm")
workpath=path.dirname(__file__)
Text_paths=[]
for i in range(4):
Text_paths.append(workpath+"\\"+text_condition[i]+"\\"+"Text")
def readTxt_list(lines):
value_list=[]
attribute=[0 for n in range(16)]
recyle_time=True
for all_line in lines:
line=all_line.split()
if len(line)>=2:
if line[0]=="All" and line[1]=="frames":
if len(line)>=5 and line[5]=="points:":
attribute[0]=int(line[6])
if line[2]=="geometry" and line[3]=="bits:":
attribute[2]=int(line[4])
if line[2]=="attributes" and line[3]=="bits:":
attribute[3]=int(line[4])
if line[2]=="total" and line[4]=="size:":
attribute[1]=int(line[5])
if line[2]=="total" and line[5]=="(user):":
if recyle_time==True:
attribute[14]=float(line[6])
recyle_time=False
else:
attribute[15]=float(line[6])
recyle_time=True
if line[2]=="MD5" and line[4]=="status:":
value_list.append(attribute)
attribute=["0" for n in range(16)]
elif line[0]=="D1_PSNR_Ave":
attribute[4]=float(line[2])
elif line[0]=="D1_HausdorffPSNR":
attribute[9]=float(line[2])
elif line[0]=="c[0]_PSNR_Ave":
attribute[5]=float(line[2])
elif line[0]=="c[0]_HausdorffPSNR_Ave":
attribute[10]=float(line[2])
elif line[0]=="c[1]_PSNR_Ave":
attribute[6]=float(line[2])
elif line[0]=="c[1]_HausdorffPSNR_Ave":
attribute[11]=float(line[2])
elif line[0]=="c[2]_PSNR_Ave":
attribute[7]=float(line[2])
elif line[0]=="c[2]_HausdorffPSNR_Ave":
attribute[12]=float(line[2])
elif line[0]=="rel_PSNR_Ave":
attribute[8]=float(line[2])
elif line[0]=="rel_HausdroffPSNR_Ave":
attribute[13]=float(line[2])
return value_list
def text_to_excel(sheet,attribute_list,tile_infor,excel_name,row,ply_name,xls,output_name):
sheet.write(row,0,ply_name)
for list_data in attribute_list:
column=1
for data in list_data:
sheet.write(row,column,data)
column+=1
row+=1
def read_txt(text_condition,output_name):
xls=xlwt.Workbook(encoding="ANSI")
for condition in text_condition:
sheet=xls.add_sheet(condition)
tile_infor=["points","Total","geometry","attributes","D1_PSNR",\
"Y_PSNR","U_PSNR","V_PSNR","R_PSNR","D1_Haus",\
"Y_Haus","U_Haus","V_Haus","R_Haus",\
"encoder_time","decoder_time"]
ls=1
for head in tile_infor:
sheet.write(0,ls,head)
ls+=1
row=1
for name in ply_name:
for B in bin_kind:
_dir=workpath+"\\"+condition+"\\"+"Text"+"\\"
if os.path.exists(_dir+"\\"+name+"_"+B+".txt"):
file_txt=open(_dir+"\\"+name+"_"+B+".txt",'r')
value_list=[]
lines=file_txt.readlines()
value_list=readTxt_list(lines)
##text_to_excel(sheet,value_list,tile_infor,name+"_"+B,row,name,xls,output_name)
sheet.write(row,0,name+"_"+B)
for list_data in value_list:
column=1
for data in list_data:
sheet.write(row,column,data)
column+=1
row+=1
xls.save(workpath+"\\"+output_name+".xls")
read_txt(text_condition,"result")
利用python提取txt并且抓取有用信息到excel
最新推荐文章于 2023-07-09 23:46:40 发布