歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Python測試之截取文件中的測試結果數據

Python測試之截取文件中的測試結果數據

日期:2017/3/1 10:04:17   编辑:Linux編程

最近在對地圖優化後的效率進行測試,andriod工程打出來的日志類似如下格式:

file: log.txt

12-26 13:23:15.702: D/sys_dbgprintf(9521): KKKKKKKKKKKKKK ^-^ draw time# 69
12-26 13:23:22.007: D/sys_dbgprintf(9521): KKKKKKKKKKKKKK ^-^ draw time# 105
12-26 13:23:22.179: D/sys_dbgprintf(9521): KKKKKKKKKKKKKK ^-^ draw time# 115
12-26 13:23:22.843: D/sys_dbgprintf(9521): KKKKKKKKKKKKKK ^-^ draw time# 168
12-26 13:23:23.054: D/sys_dbgprintf(9521): KKKKKKKKKKKKKK ^-^ draw time# 149
12-26 13:23:23.179: D/sys_dbgprintf(9521): KKKKKKKKKKKKKK ^-^ draw time# 78
12-26 13:23:23.343: D/sys_dbgprintf(9521): KKKKKKKKKKKKKK ^-^ draw time# 103
12-26 13:23:25.734: D/sys_dbgprintf(9521): KKKKKKKKKKKKKK ^-^ draw time# 112
12-26 13:23:25.874: D/sys_dbgprintf(9521): KKKKKKKKKKKKKK ^-^ draw time# 91
Stitch Time
12-26 13:23:35.788: D/sys_dbgprintf(9521): KKKKKKKKKKKKKK ^-^ draw time# 109
12-26 13:23:36.187: D/sys_dbgprintf(9521): KKKKKKKKKKKKKK ^-^ draw time# 183
12-26 13:23:36.327: D/sys_dbgprintf(9521): KKKKKKKKKKKKKK ^-^ draw time# 89
12-26 13:23:36.460: D/sys_dbgprintf(9521): KKKKKKKKKKKKKK ^-^ draw time# 81
12-26 13:23:36.616: D/sys_dbgprintf(9521): KKKKKKKKKKKKKK ^-^ draw time# 103

現在需要從裡面把#後面的數字給截取出來,然後放到Excel中,求取平均值;同事就這樣做起來了,然後我想了下,這樣一個一個的從中復制出來,太慢了。因此萌生了一個想法,寫個腳本截取出來不就好了嗎?用C的話,太麻煩了,就為了一個測試,寫個c,不值得,費勁。以前學過python,那就python吧,搗鼓搗鼓還搗鼓好了。雖然寫的很亂(而且不高效),但是能出來就好:

# -*- coding: utf-8 -*-
#! /usr/bin/env python
# file: log.py
# 時間獲取測試腳本;
# 以'#'作為分隔符,如:
# 12-25 12:25:50.953: D/sys_dbgprintf(32417):AAAAAAAAAAAAAAAAAAAAAA ^_^ draw times#73
# 最後截取到73,並追加到輸出文件,方便粘貼到excell中,計算.
#
#
#使用方式: python log.py -i 測試文件 -o 輸出文件
import sys, getopt, os, string
opts, args = getopt.getopt(sys.argv[1:], "hi:o:")
input_file = ""
output_file = ""
#function
def usage(message):
print message
def handle(src, dst):
if os.path.exists(dst):
os.remove(dst)
file_in = open(src, "r")
file_out = open(dst, "a")
try:
for line in file_in:
spStr = line.split('#')
#tmp = spStr[1].strip('\n')
#print "hhhhhh", spStr
list_length = len(spStr)
#print "qqqqq", list_length
if list_length != 2:
if list_length == 1:
tmp = spStr[0]
file_out.write(tmp)
continue
tmp = spStr[1]
if tmp != "":
file_out.write(tmp)
#print tmp
finally:
file_in.close()
file_out.close()
for op, value in opts:
if op == "-i":
input_file = value
elif op == "-o":
output_file = value
elif op == "-h":
usage("Using: python log.py -i input_file -o outputfile")
sys.exit()
if input_file == "" or input_file == "-o":
usage("no input file")
sys.exit()
print "測試文件:", input_file
if output_file == "":
usage("no output file")
sys.exit()
print "輸出文件:", output_file
#get time(int type) from each in lines
handle(input_file, output_file)

運行以下:python log.py -i log.txt -o out.txt

得到類似如下結果:

103

112

91

Stitch Time

109

..... 這樣就可以拷貝需要的往表格一粘貼就完事了...

Copyright © Linux教程網 All Rights Reserved