Python测试之截取文件中的测试结果数据

pythonhuohuo 2012-12-28

最近在对地图优化后的效率进行测试,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

..... 这样就可以拷贝需要的往表格一粘贴就完事了...

相关推荐