python 每日一练: 读取log文件中的数据,并画图表
之前在excel里面分析log数据,简直日了*了。 现在用python在处理日志数据.
主要涉及 matplotlib,open和循环的使用。
日志内容大致如下
2016-10-21 21:07:59,787 [7 MainWindowForm]INFO: update time 136.6314
2016-10-21 21:07:59,908 [7 KinectServer]INFO: lClientSockets[0] elapsed time 16.
2016-10-21 21:07:59,918 [7 KinectServer]INFO: lClientSockets[1] elapsed time 107.
2016-10-21 21:07:59,929 [7 MainWindowForm]INFO: update time 135.1311
2016-10-21 21:08:00,039 [7 KinectServer]INFO: lClientSockets[0] elapsed time 14.
2016-10-21 21:08:00,045 [7 KinectServer]INFO: lClientSockets[1] elapsed time 103.
2016-10-21 21:08:00,053 [7 MainWindowForm]INFO: update time 118.1132
python处理代码
import matplotlib.pyplot as plt
input = open('serverlog.txt', 'r')
rangeUpdateTime = [0.0]
for line in input:
line = line.split()
if 'update' in line:
rangeUpdateTime.append(float(line[-1]))
plt.figure('frame time')
plt.subplot(211)
plt.plot(rangeUpdateTime, '.r',)
plt.grid(True)
plt.subplot(212)
plt.plot(rangeUpdateTime)
plt.grid(True)
plt.show()
结果
真心是又好又快出结果_