[2010年06月12日] python 小练:抽取日志中的速度
时间:2010-06-13 来源:oychw
要点:格式化输出,这点要加强总结。服务器上面的python是2.4.*的,不支持format,只好使用'%10.2f' % (sums[item][0]/count)这样的格式。切记(sums[item][0]/count)要使用括号括起来,涉及到运算优先级
#!/usr/bin/env python
# -*- coding: gbk -*-
import re
import sys
import glob
#gtalk: xurongzhong#gmail.com
#设定字符编码为GBK
reload(sys)
sys.setdefaultencoding('gbk')
#匹配速度和日期的正则表达式
reg_speed = re.compile('FSU_TEXT_DOC_PROCESS_SPEED=(.*?)M/S')
reg_date = re.compile('\d+-\d+-\d+')
Lspeeds = []
dates = []
sums = {}
for filename in glob.glob("twse.fsu.log*"):
#针对每个文件提取顺排速度
for line in open(filename):
speeds = reg_speed.findall(line)
if speeds:
Lspeeds.append(speeds[0])
date = reg_date.findall(line)
dates.append(date[0])
if str(date[0]) not in sums:
sums[str(date[0])] = [0,0,0]
if float(speeds[0]) >= 0.2:
sums[str(date[0])][2] +=1
elif float(speeds[0]) <= 0.1:
sums[str(date[0])][0] +=1
else:
sums[str(date[0])][1] +=1
print "time\t\t","<=0.1\t"," 0.1~0.2\t",">=0.2"
for item in sums:
count = float(sum(sums[item]))
print item,
#切记(sums[item][0]/count)要使用括号括起来,涉及到运算优先级
print '%10.2f' % (sums[item][0]/count),'%10.2f' % (sums[item][1]/count),'%10.2f' % (sums[item][2]/count)
#!/usr/bin/env python
# -*- coding: gbk -*-
import re
import sys
import glob
#gtalk: xurongzhong#gmail.com
#设定字符编码为GBK
reload(sys)
sys.setdefaultencoding('gbk')
#匹配速度和日期的正则表达式
reg_speed = re.compile('FSU_TEXT_DOC_PROCESS_SPEED=(.*?)M/S')
reg_date = re.compile('\d+-\d+-\d+')
Lspeeds = []
dates = []
sums = {}
for filename in glob.glob("twse.fsu.log*"):
#针对每个文件提取顺排速度
for line in open(filename):
speeds = reg_speed.findall(line)
if speeds:
Lspeeds.append(speeds[0])
date = reg_date.findall(line)
dates.append(date[0])
if str(date[0]) not in sums:
sums[str(date[0])] = [0,0,0]
if float(speeds[0]) >= 0.2:
sums[str(date[0])][2] +=1
elif float(speeds[0]) <= 0.1:
sums[str(date[0])][0] +=1
else:
sums[str(date[0])][1] +=1
print "time\t\t","<=0.1\t"," 0.1~0.2\t",">=0.2"
for item in sums:
count = float(sum(sums[item]))
print item,
#切记(sums[item][0]/count)要使用括号括起来,涉及到运算优先级
print '%10.2f' % (sums[item][0]/count),'%10.2f' % (sums[item][1]/count),'%10.2f' % (sums[item][2]/count)
相关阅读 更多 +