Chinaunix首页 | 论坛 | 博客
  • 博客访问: 499596
  • 博文数量: 401
  • 博客积分: 244
  • 博客等级: 入伍新兵
  • 技术积分: 2215
  • 用 户 组: 普通用户
  • 注册时间: 2012-08-04 10:02
文章分类

全部博文(401)

文章存档

2013年(37)

2012年(364)

分类:

2013-01-04 10:11:59

  最近在对地图优化后的效率进行测试,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吧,捣鼓捣鼓还捣鼓好了。虽然写的很乱(而且不高效),但是能出来就好:

点击(此处)折叠或打开

  1. # -*- coding: utf-8 -*-
  2. #! /usr/bin/env python

  3. # file: log.py
  4. # 时间获取测试脚本;
  5. # 以'#'作为分隔符,如:
  6. # 12-25 12:25:50.953: D/sys_dbgprintf(32417):AAAAAAAAAAAAAAAAAAAAAA ^_^ draw times#73
  7. # 最后截取到73,并追加到输出文件,方便粘贴到excell中,计算.
  8. #
  9. #
  10. #使用方式: python log.py -i 测试文件 -o 输出文件

  11. import sys, getopt, os, string

  12. opts, args = getopt.getopt(sys.argv[1:], "hi:o:")
  13. input_file = ""
  14. output_file = ""

  15. #function
  16. def usage(message):
  17.     print message
  18. def handle(src, dst):
  19.     if os.path.exists(dst):
  20.         os.remove(dst)
  21.     file_in = open(src, "r")
  22.     file_out = open(dst, "a")
  23.     try:
  24.         for line in file_in:
  25.             spStr = line.split('#')
  26.             #tmp = spStr[1].strip('\n')
  27.             #print "hhhhhh", spStr
  28.             list_length = len(spStr)
  29.             #print "qqqqq", list_length
  30.             if list_length != 2:
  31.                 if list_length == 1:
  32.                         tmp = spStr[0]
  33.                         file_out.write(tmp)
  34.                 continue
  35.             tmp = spStr[1]
  36.             if tmp != "":
  37.                 file_out.write(tmp)
  38.             #print tmp
  39.     finally:
  40.         file_in.close()
  41.         file_out.close()

  42. for op, value in opts:
  43.     if op == "-i":
  44.         input_file = value
  45.     elif op == "-o":
  46.         output_file = value
  47.     elif op == "-h":
  48.         usage("Using: python log.py -i input_file -o outputfile")
  49.         sys.exit()

  50. if input_file == "" or input_file == "-o":
  51.     usage("no input file")
  52.     sys.exit()
  53. print "测试文件:", input_file

  54. if output_file == "":
  55.     usage("no output file")
  56.     sys.exit()
  57. print "输出文件:", output_file

  58. #get time(int type) from each in lines
  59. handle(input_file, output_file)
运行以下:python log.py -i log.txt -o out.txt
得到类似如下结果:
103
112
91
Stitch Time
109
..... 这样就可以拷贝需要的往表格一粘贴就完事了...
真个故事就是这样....
阅读(553) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~