Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2552196
  • 博文数量: 315
  • 博客积分: 3901
  • 博客等级: 少校
  • 技术积分: 3640
  • 用 户 组: 普通用户
  • 注册时间: 2011-05-08 15:32
个人简介

知乎:https://www.zhihu.com/people/monkey.d.luffy Android高级开发交流群2: 752871516

文章分类

全部博文(315)

文章存档

2019年(2)

2018年(1)

2016年(7)

2015年(32)

2014年(39)

2013年(109)

2012年(81)

2011年(44)

分类: Python/Ruby

2012-12-27 20:22:06

  最近在对地图优化后的效率进行测试,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
..... 这样就可以拷贝需要的往表格一粘贴就完事了...
真个故事就是这样....
阅读(2942) | 评论(4) | 转发(3) |
0

上一篇:Genius Singer

下一篇:grep、find、awk

给主人留下些什么吧!~~

HuangLei_LEGO2013-01-02 18:17:43

吴秦: 不是要求平均值吗?为什么不直接算好,python截到数据还要放到excel中去求.....
日志是必须要截取的。测试后的数据是要做成文档的,excell求平均值很方便的。这里目的就是从里面拿数据...

吴秦2013-01-02 13:17:24

不是要求平均值吗?为什么不直接算好,python截到数据还要放到excel中去求

HuangLei_LEGO2012-12-31 19:34:35

YourEyes: 这个……
一条正则就可以了吧.....
哦,不过很长时间没用了,不知道怎么写,就临时整了个。能帮整个不?谢谢.

YourEyes2012-12-31 09:26:12

这个……
一条正则就可以了吧