log日志格式如下,自己拼吧
2016-10-21 21:08:00,039 [7 KinectServer]INFO: 14
2016-10-21 21:08:00,045 [7 KinectServer]INFO: 103
2016-10-21 21:08:00,053 [7 MainWindowForm]INFO: 118.1132
# 主要是讲readline()和readlines()的区别,readlines读取到的是所有行组成的列表,readline读取的是文件中的一行
def count_avg_time2():
try:
list_last_column = []
total = 0
avg = 0
with open("jcl.log", "r", encoding="utf-8") as f:
for line in f.readlines():
line = [i for i in line.split()]
if not line:
break
list_last_column.append(float(line[-1]))
# return list_last_column
if list_last_column:
for i in list_last_column:
total = total + i
avg = total / len(list_last_column)
return avg
except Exception as e:
print(e)
def count_avg_time3():
try:
list_last_column = []
total = 0
avg = 0
with open("jcl.log", "r", encoding="utf-8") as f:
while True:
# 这是readline()
lines = f.readline()
# print(f.readlines())
# print(type(lines))
if not lines:
break
item = [i for i in lines.split()]
list_last_column.append(float(item[-1]))
if list_last_column:
for i in list_last_column:
total = total + i
avg = total / len(list_last_column)
return avg
except Exception as e:
print(e)
if __name__ == "__main__":
# avg_time2 = count_avg_time2()
# print(avg_time2)
avg_time3 = count_avg_time3()
print(avg_time3)