python简单文本处理的方法

时间:2021-05-22

本文实例讲述了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-0avgFirstDraw = 0; #8-2avgLoadImage = 0; #11-5avgSecondDraw = 0;#13-10fobj = open("F:\log.txt","r")imageTimeSta = {}dic = {}path = ""idx = 0for 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] = timefobj.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 5124D:\pics\测试图片\解码性能对比用图\jpeg\较小图\2.JPGenter OpenImage at 5124In OpenImage send On_ImageRefresh at 5124enter LoadImage at 5124leave OpenImage at 5124leave OpenNextImage at 5124decode began at 5124enter DrawClient at 5140leave DrawClient at 5155decode end at 5265in LoadImage send On_ImageRefresh at 5265leave loadImage at 5265enter DrawClient at 5280leave DrawClient at 5327==================================================================================enter OpenNextImage at 6280D:\pics\测试图片\解码性能对比用图\jpeg\较小图\3.JPGenter OpenImage at 6280In OpenImage send On_ImageRefresh at 6280enter LoadImage at 6280leave OpenImage at 6296leave OpenNextImage at 6296decode began at 6296enter DrawClient at 6296leave DrawClient at 6312decode end at 6437in LoadImage send On_ImageRefresh at 6437enter DrawClient at 6437leave loadImage at 6452leave DrawClient at 6499==================================================================================enter OpenNextImage at 7265D:\pics\测试图片\解码性能对比用图\jpeg\较小图\4.JPGenter OpenImage at 7265In OpenImage send On_ImageRefresh at 7265leave OpenImage at 7265leave OpenNextImage at 7265enter LoadImage at 7265decode began at 7265enter DrawClient at 7265leave DrawClient at 7296decode end at 7421in LoadImage send On_ImageRefresh at 7421enter DrawClient at 7421leave loadImage at 7437leave DrawClient at 7483==================================================================================enter OpenNextImage at 8062D:\pics\测试图片\解码性能对比用图\jpeg\较小图\5.JPGenter OpenImage at 8062In OpenImage send On_ImageRefresh at 8062leave OpenImage at 8062leave OpenNextImage at 8062enter LoadImage at 8062decode began at 8062enter DrawClient at 8062leave DrawClient at 8077decode end at 8202in LoadImage send On_ImageRefresh at 8202enter DrawClient at 8202leave DrawClient at 8265leave loadImage at 8280==================================================================================enter OpenNextImage at 8811D:\pics\测试图片\解码性能对比用图\jpeg\较小图\6.JPGenter OpenImage at 8811In OpenImage send On_ImageRefresh at 8811leave OpenImage at 8811leave OpenNextImage at 8811enter LoadImage at 8811decode began at 8811enter DrawClient at 8811leave DrawClient at 8843decode end at 8968in LoadImage send On_ImageRefresh at 8968leave loadImage at 8968enter DrawClient at 8968leave DrawClient at 9030==================================================================================enter OpenNextImage at 9515D:\pics\测试图片\解码性能对比用图\jpeg\较小图\7.JPGenter OpenImage at 9515In OpenImage send On_ImageRefresh at 9515leave OpenImage at 9515leave OpenNextImage at 9515enter LoadImage at 9515decode began at 9530enter DrawClient at 9530leave DrawClient at 9546decode end at 9671in LoadImage send On_ImageRefresh at 9671enter DrawClient at 9671leave loadImage at 9671leave DrawClient at 9733==================================================================================enter OpenNextImage at 10171D:\pics\测试图片\解码性能对比用图\jpeg\较小图\8.JPGenter OpenImage at 10171In OpenImage send On_ImageRefresh at 10171leave OpenImage at 10171leave OpenNextImage at 10171enter LoadImage at 10171decode began at 10186enter DrawClient at 10186leave DrawClient at 10202decode end at 10311in LoadImage send On_ImageRefresh at 10311leave loadImage at 10311enter DrawClient at 10311leave DrawClient at 10374==================================================================================enter OpenNextImage at 10811D:\pics\测试图片\解码性能对比用图\jpeg\较小图\9.JPGenter OpenImage at 10811In OpenImage send On_ImageRefresh at 10811enter LoadImage at 10811leave OpenImage at 10811leave OpenNextImage at 10811enter DrawClient at 10811decode began at 10811leave DrawClient at 10843decode end at 10952in LoadImage send On_ImageRefresh at 10952leave loadImage at 10952enter DrawClient at 10952leave DrawClient at 11030==================================================================================enter OpenNextImage at 11452D:\pics\测试图片\解码性能对比用图\jpeg\较小图\10.JPGenter OpenImage at 11452In OpenImage send On_ImageRefresh at 11452leave OpenImage at 11452leave OpenNextImage at 11452enter LoadImage at 11452decode began at 11452enter DrawClient at 11468leave DrawClient at 11483decode end at 11593in LoadImage send On_ImageRefresh at 11593enter DrawClient at 11593leave loadImage at 11608leave DrawClient at 11655==================================================================================enter OpenNextImage at 12077enter DrawClient at 12077leave DrawClient at 12108==================================================================================enter OpenNextImage at 13124D:\pics\测试图片\解码性能对比用图\jpeg\较小图\1.jpgenter OpenImage at 13124In OpenImage send On_ImageRefresh at 13124leave OpenImage at 13124leave OpenNextImage at 13124enter LoadImage at 13124decode began at 13124enter DrawClient at 13139leave DrawClient at 13155decode end at 13358in LoadImage send On_ImageRefresh at 13358leave loadImage at 13358enter DrawClient at 13358leave DrawClient at 13405==================================================================================

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

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章