python简单文本处理的方法


Posted in Python onJuly 10, 2015

本文实例讲述了python简单文本处理的方法。分享给大家供大家参考。具体如下:

由于有多线程的影响,c++项目打印出来的时间顺序不一致,导致不太好在excel中统计,故使用python写了段脚本来解决之。涉及到如下方面

1. txt文本的读取,utf8的处理
2. 字符串的基本操作
3. dict的基本操作
4. list(数组)的基本操作

#!/usr/bin/python
#print "Hello World"
str_seperator = "=================================================================================="
timePointName = ["enter OpenNextImage at",#0
     "enter OpenImage at",#1
     "In OpenImage send On_ImageRefresh at",#2
     "leave OpenImage at",#3
     "leave OpenNextImage at",#4
     "enter LoadImage at",#5
     "decode began at",#6
     "enter DrawClient at",#7
     "leave DrawClient at",#8
     "decode end at",#9
     "in LoadImage send On_ImageRefresh at",#10
     "leave loadImage at",#11
     "second enter DrawClient at",#12
     "second leave DrawClient at" #13
     ]
itemNumber= 0;
avgTotal = 0; #13-0
avgFirstDraw = 0; #8-2
avgLoadImage = 0; #11-5
avgSecondDraw = 0;#13-10
fobj = open("F:\log.txt","r")
imageTimeSta = {}
dic = {}
path = ""
idx = 0
for line in fobj:
  idx = idx + 1
  if idx == 1:
    line = line[3:]
  else:
    pass
  line = line.strip()
  line = line.decode("utf-8").encode("gbk")
  if line == str_seperator:
    if path == "":
      pass
    else:
      imageTimeSta[path] = dic
    dic = {}
    path = ""
    continue
  tabIndex = line.find('\t')
  if tabIndex == -1:
    path = line
    print path
    continue
  tabLastIndex = line.rfind('\t')
  name = line[0:tabIndex]
  time = int(line[tabLastIndex + 1:])
  if name in dic:
    dic["second " + name] = time
  else:
    dic[name] = time
fobj.close()
itemNumber = len(imageTimeSta)
keys = imageTimeSta.keys();
for (k,dic) in imageTimeSta.iteritems():
  avgTotal += dic[timePointName[13]] - dic[timePointName[0]];
  avgFirstDraw += dic[timePointName[8]] - dic[timePointName[2]];
  avgLoadImage += dic[timePointName[11]] - dic[timePointName[5]];
  avgSecondDraw += dic[timePointName[13]] - dic[timePointName[10]];
print 'avgTotal',avgTotal / float(itemNumber)
print 'avgFirstDraw',avgFirstDraw / float(itemNumber)
print 'avgLoadImage',avgLoadImage / float(itemNumber)
print 'avgSecondDraw',avgSecondDraw / float(itemNumber)
#print imageTimeSta

log.txt文件如下:

enter OpenNextImage at  5124
D:\pics\测试图片\解码性能对比用图\jpeg\较小图\2.JPG
enter OpenImage at  5124
In OpenImage send On_ImageRefresh at  5124
enter LoadImage at  5124
leave OpenImage at  5124
leave OpenNextImage at  5124
decode began at  5124
enter DrawClient at  5140
leave DrawClient at  5155
decode end at  5265
in LoadImage send On_ImageRefresh at  5265
leave loadImage at  5265
enter DrawClient at  5280
leave DrawClient at  5327
==================================================================================
enter OpenNextImage at  6280
D:\pics\测试图片\解码性能对比用图\jpeg\较小图\3.JPG
enter OpenImage at  6280
In OpenImage send On_ImageRefresh at  6280
enter LoadImage at  6280
leave OpenImage at  6296
leave OpenNextImage at  6296
decode began at  6296
enter DrawClient at  6296
leave DrawClient at  6312
decode end at  6437
in LoadImage send On_ImageRefresh at  6437
enter DrawClient at  6437
leave loadImage at  6452
leave DrawClient at  6499
==================================================================================
enter OpenNextImage at  7265
D:\pics\测试图片\解码性能对比用图\jpeg\较小图\4.JPG
enter OpenImage at  7265
In OpenImage send On_ImageRefresh at  7265
leave OpenImage at  7265
leave OpenNextImage at  7265
enter LoadImage at  7265
decode began at  7265
enter DrawClient at  7265
leave DrawClient at  7296
decode end at  7421
in LoadImage send On_ImageRefresh at  7421
enter DrawClient at  7421
leave loadImage at  7437
leave DrawClient at  7483
==================================================================================
enter OpenNextImage at  8062
D:\pics\测试图片\解码性能对比用图\jpeg\较小图\5.JPG
enter OpenImage at  8062
In OpenImage send On_ImageRefresh at  8062
leave OpenImage at  8062
leave OpenNextImage at  8062
enter LoadImage at  8062
decode began at  8062
enter DrawClient at  8062
leave DrawClient at  8077
decode end at  8202
in LoadImage send On_ImageRefresh at  8202
enter DrawClient at  8202
leave DrawClient at  8265
leave loadImage at  8280
==================================================================================
enter OpenNextImage at  8811
D:\pics\测试图片\解码性能对比用图\jpeg\较小图\6.JPG
enter OpenImage at  8811
In OpenImage send On_ImageRefresh at  8811
leave OpenImage at  8811
leave OpenNextImage at  8811
enter LoadImage at  8811
decode began at  8811
enter DrawClient at  8811
leave DrawClient at  8843
decode end at  8968
in LoadImage send On_ImageRefresh at  8968
leave loadImage at  8968
enter DrawClient at  8968
leave DrawClient at  9030
==================================================================================
enter OpenNextImage at  9515
D:\pics\测试图片\解码性能对比用图\jpeg\较小图\7.JPG
enter OpenImage at  9515
In OpenImage send On_ImageRefresh at  9515
leave OpenImage at  9515
leave OpenNextImage at  9515
enter LoadImage at  9515
decode began at  9530
enter DrawClient at  9530
leave DrawClient at  9546
decode end at  9671
in LoadImage send On_ImageRefresh at  9671
enter DrawClient at  9671
leave loadImage at  9671
leave DrawClient at  9733
==================================================================================
enter OpenNextImage at  10171
D:\pics\测试图片\解码性能对比用图\jpeg\较小图\8.JPG
enter OpenImage at  10171
In OpenImage send On_ImageRefresh at  10171
leave OpenImage at  10171
leave OpenNextImage at  10171
enter LoadImage at  10171
decode began at  10186
enter DrawClient at  10186
leave DrawClient at  10202
decode end at  10311
in LoadImage send On_ImageRefresh at  10311
leave loadImage at  10311
enter DrawClient at  10311
leave DrawClient at  10374
==================================================================================
enter OpenNextImage at  10811
D:\pics\测试图片\解码性能对比用图\jpeg\较小图\9.JPG
enter OpenImage at  10811
In OpenImage send On_ImageRefresh at  10811
enter LoadImage at  10811
leave OpenImage at  10811
leave OpenNextImage at  10811
enter DrawClient at  10811
decode began at  10811
leave DrawClient at  10843
decode end at  10952
in LoadImage send On_ImageRefresh at  10952
leave loadImage at  10952
enter DrawClient at  10952
leave DrawClient at  11030
==================================================================================
enter OpenNextImage at  11452
D:\pics\测试图片\解码性能对比用图\jpeg\较小图\10.JPG
enter OpenImage at  11452
In OpenImage send On_ImageRefresh at  11452
leave OpenImage at  11452
leave OpenNextImage at  11452
enter LoadImage at  11452
decode began at  11452
enter DrawClient at  11468
leave DrawClient at  11483
decode end at  11593
in LoadImage send On_ImageRefresh at  11593
enter DrawClient at  11593
leave loadImage at  11608
leave DrawClient at  11655
==================================================================================
enter OpenNextImage at  12077
enter DrawClient at  12077
leave DrawClient at  12108
==================================================================================
enter OpenNextImage at  13124
D:\pics\测试图片\解码性能对比用图\jpeg\较小图\1.jpg
enter OpenImage at  13124
In OpenImage send On_ImageRefresh at  13124
leave OpenImage at  13124
leave OpenNextImage at  13124
enter LoadImage at  13124
decode began at  13124
enter DrawClient at  13139
leave DrawClient at  13155
decode end at  13358
in LoadImage send On_ImageRefresh at  13358
leave loadImage at  13358
enter DrawClient at  13358
leave DrawClient at  13405
==================================================================================

希望本文所述对大家的Python程序设计有所帮助。

Python 相关文章推荐
Python 变量类型及命名规则介绍
Jun 08 Python
python内存管理分析
Apr 08 Python
Python利用Beautiful Soup模块创建对象详解
Mar 27 Python
numpy返回array中元素的index方法
Jun 27 Python
python 找出list中最大或者最小几个数的索引方法
Oct 30 Python
使用python根据端口号关闭进程的方法
Nov 06 Python
在Python中使用turtle绘制多个同心圆示例
Nov 23 Python
Django通用类视图实现忘记密码重置密码功能示例
Dec 17 Python
pytorch方法测试详解——归一化(BatchNorm2d)
Jan 15 Python
pytorch模型存储的2种实现方法
Feb 14 Python
Python集成开发工具Pycharm的安装和使用详解
Mar 18 Python
如何利用Matlab制作一款真正的拼图小游戏
May 11 Python
Python实现把json格式转换成文本或sql文件
Jul 10 #Python
Python中的一些陷阱与技巧小结
Jul 10 #Python
Python中的fileinput模块的简单实用示例
Jul 09 #Python
Python中的anydbm模版和shelve模版使用指南
Jul 09 #Python
python冒泡排序简单实现方法
Jul 09 #Python
python基于BeautifulSoup实现抓取网页指定内容的方法
Jul 09 #Python
python简单猜数游戏实例
Jul 09 #Python
You might like
php中的观察者模式
2010/03/24 PHP
CI(CodeIgniter)框架中的增删改查操作
2014/06/10 PHP
[原创]php逐行读取txt文件写入数组的方法
2015/07/02 PHP
js最简单的拖拽效果实现代码
2010/09/24 Javascript
javascript中怎么做对象的类型判断
2013/11/11 Javascript
JavaScript设置首页和收藏页面的小例子
2013/11/11 Javascript
jquery选择器之内容过滤选择器详解
2014/01/27 Javascript
js 调用百度地图api并在地图上进行打点添加标注
2014/05/13 Javascript
JavaScript组合拼接字符串的效率对比测试
2014/11/06 Javascript
yui3的AOP(面向切面编程)和OOP(面向对象编程)
2015/05/01 Javascript
Nodejs中session的简单使用及通过session实现身份验证的方法
2016/02/04 NodeJs
jQuery简单实现tab选项卡切换效果
2016/06/20 Javascript
JS获取html元素的标记名实现方法
2016/10/08 Javascript
jQuery实现简单的滑动导航代码(移动端)
2017/05/22 jQuery
微信小程序模板和模块化用法实例分析
2017/11/28 Javascript
element-ui上传一张图片后隐藏上传按钮功能
2019/05/22 Javascript
在vue和element-ui的table中实现分页复选功能
2019/12/04 Javascript
Python3实现Web网页图片下载
2016/01/28 Python
用python 批量更改图像尺寸到统一大小的方法
2018/03/31 Python
python opencv 图像尺寸变换方法
2018/04/02 Python
python模拟表单提交登录图书馆
2018/04/27 Python
python3实现钉钉消息推送的方法示例
2019/03/14 Python
Python获取当前脚本文件夹(Script)的绝对路径方法代码
2019/08/27 Python
python匿名函数的使用方法解析
2019/10/10 Python
python实现两个字典合并,两个list合并
2019/12/02 Python
matplotlib实现显示伪彩色图像及色度条
2019/12/07 Python
Python编译为二进制so可执行文件实例
2019/12/23 Python
什么是python类属性
2020/06/10 Python
Python面向对象特殊属性及方法解析
2020/09/16 Python
python爬虫泛滥的解决方法详解
2020/11/25 Python
欧洲最大的拼图游戏商店:JigsawPuzzle.co.uk
2018/07/04 全球购物
英国手工制作的现代与经典的沙发和床:Love Your Home
2020/09/26 全球购物
西安众合通用.net笔试题
2013/03/18 面试题
高中军训感言600字
2014/03/11 职场文书
2019大学生暑期实习心得总结
2019/08/21 职场文书
Python pygame实现中国象棋单机版源码
2021/06/20 Python