__author__ = 'Administrator'
import xlsxwriter as writer
import xlrd as read
CPUFile='SAR_CPU_CMP.log'
Key_CPU_name=["all","0","1","2","3","4","5","6","7"]
jcc=[]
DataTime=[]
DataTime1=[]
CPU_ALL=[]
CPU0=[]
CPU1=[]
CPU2=[]
CPU3=[]
CPU4=[]
CPU5=[]
CPU6=[]
CPU7=[]
flag=False
def main():
j=0
flag=False
try:
fin=open(CPUFile,'r')
except:
print "Cannot open file:"+CPUFile
exit
while 1:
line=fin.readline()
if not line:
break
jcc=line.replace("\n","").split("\t")
#print jcc
if len(jcc)== 8 :
DataTime.append(jcc[0])
if jcc[1]== Key_CPU_name[0]:
CPU_ALL.append(jcc[2]+jcc[4])
j=j+1
if jcc[1]==Key_CPU_name[1]:
CPU0.append(jcc[2]+jcc[4])
j=j+1
if jcc[1]==Key_CPU_name[2]:
CPU1.append(jcc[2]+jcc[4])
j=j+1
if jcc[1]==Key_CPU_name[3]:
CPU2.append(jcc[2]+jcc[4])
j=j+1
if jcc[1]==Key_CPU_name[4]:
CPU3.append(jcc[2]+jcc[4])
j=j+1
if jcc[1]==Key_CPU_name[5]:
CPU4.append(jcc[2]+jcc[4])
j=j+1
if jcc[1]==Key_CPU_name[6]:
CPU5.append(jcc[2]+jcc[4])
j=j+1
if jcc[1]==Key_CPU_name[7]:
CPU6.append(jcc[2]+jcc[4])
j=j+1
if jcc[1]==Key_CPU_name[8]:
CPU7.append(jcc[2]+jcc[4])
j=j+1
if len(jcc)==5:
flag=True
DataTime.append(jcc[0])
if jcc[1]== Key_CPU_name[0]:
CPU_ALL.append(jcc[2]+jcc[4])
j=j+1
if jcc[1]==Key_CPU_name[1]:
CPU0.append(jcc[2]+jcc[4])
j=j+1
if jcc[1]==Key_CPU_name[2]:
CPU1.append(jcc[2]+jcc[4])
j=j+1
if jcc[1]==Key_CPU_name[3]:
CPU2.append(jcc[2]+jcc[4])
j=j+1
if jcc[1]==Key_CPU_name[4]:
CPU3.append(jcc[2]+jcc[4])
j=j+1
if not flag:
for m in range(0,j-1,9):
DataTime1.append(DataTime[m])
else:
for m in range(0,j-1,5):
DataTime1.append(DataTime[m])
workbook = writer.Workbook('jcc.xls')
worksheet = workbook.add_worksheet('cpuinfo')
bold = workbook.add_format({'bold': True})
worksheet.write(0, 0, 'TIME', bold)
worksheet.write(0, 1, 'AVGCPU', bold)
worksheet.write(0, 2, 'CPU0', bold)
worksheet.write(0, 3, 'CPU1', bold)
worksheet.write(0, 4, 'CPU2', bold)
worksheet.write(0, 5, 'CPU3', bold)
if flag:
worksheet.write(0, 6, 'CPU4', bold)
worksheet.write(0, 7, 'CPU5', bold)
worksheet.write(0, 8, 'CPU6', bold)
worksheet.write(0, 9, 'CPU7', bold)
#worksheet.write(1,0,DataTime1[0])
for h in range(0,(j/9)-1,1):
worksheet.write(h+1,0,DataTime1[h])
worksheet.write(h+1,1,CPU_ALL[h])
worksheet.write(h+1,2,CPU0[h])
worksheet.write(h+1,3,CPU1[h])
worksheet.write(h+1,4,CPU2[h])
worksheet.write(h+1,5,CPU3[h])
if flag:
worksheet.write(h+1,6,CPU4[h])
worksheet.write(h+1,7,CPU5[h])
worksheet.write(h+1,8,CPU6[h])
worksheet.write(h+1,9,CPU7[h])
workbook.close()
#file = open("sample.txt")
#while 1:
# lines = file.readlines(100000)
# if not lines:
# break
# for line in lines:
# pass # do something
if __name__=="__main__":
main()