软件下载 | 资讯教程 | 最近更新 | 下载排行 | 一键转帖 | 发布投稿
您的位置:最火下载站 > 电脑教程 > 编程开发 > Python > 使用python进行简单的文本处理

使用python进行简单的文本处理

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

1. txt文本的读取,utf8的处理

2. 字符串的基本操作

3. dict的基本操作

4.list(数组)的基本操作

Copy to ClipboardLiehuo.Net Codes引用的内容:[www.veryhuo.com]
#!/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
上一篇:暂无文档
下一篇:Python基于正向最大匹配法的分词处理程序
    相关阅读
    栏目导航
    推荐软件