python用于日志文件处理(文件读取 with open方式 readline识别是否读到末尾)
程序员文章站
2022-06-26 15:15:40
在测试中会遇到很多重复性的查日志的工作,这里用python会大大的提高工作效率,最近完成的一项工作就涉及了此类的工作背景:日志记录了信息帧的收发,我需要检查某一个寄存器的收发值,不过这一个个的查下来,恐怕我是要累吐血了,于是我就用上了python,让电脑替我干活……with open 和readline首先要读取文件,以with open的方式,这样可以在读完文件后关掉该文件,然后是readline,可以一行一行的读取,最后就是判断日志的内容了,日志内容非常规范,所以处理起来相对简单上代码:关...
在测试中会遇到很多重复性的查日志的工作,这里用python会大大的提高工作效率,最近完成的一项工作就涉及了此类的工作
- 背景:
日志记录了信息帧的收发,我需要检查某一个寄存器的收发值,不过这一个个的查下来,恐怕我是要累吐血了,于是我就用上了python,让电脑替我干活……
with open 和readline
首先要读取文件,以with open的方式,这样可以在读完文件后关掉该文件,然后是readline,可以一行一行的读取,最后就是判断日志的内容了,日志内容非常规范,所以处理起来相对简单
上代码:
关于寄存器中的值我只关心是不是0,1还是其他值,所以这样处理 - 如何判断文件读完?
x = f.readline() 此时如果x为空,则为读到了文件的末尾,因为即使是中间位置有空行,也会存在换行符/n,只有到末尾为空
代码如下
num0 = 0
num1 = 0
num2 = 0
intList = []
with open('ProtocolModbusTcp.log','r') as f :
while True:
# 每次读一行,一直读
m = f.readline()
x = m.rfind('send')
# 查找该行有没有send字符串
# 没有查到就返回-1 查到就返回一个index
if x != -1:
dataStr = m[x+5:]
intList = dataStr.split(' ')
# 通过空格将数据分开到列表中
intList = intList[0:-1]
if intList[-4] == '00' and intList[-3] == '32':
mrecv = f.readline()
recvStr = mrecv[32+5:]
recvList = recvStr.split(' ')
recvList = recvList[0:-1]
if recvList[-2] == '00' and recvList[-1] == '00':
num0 += 1
elif recvList[-2] == '00' and recvList[-1] == '01':
num1 += 1
else:
num2 += 1
if m == '':
break
# 如何判断是否读到了文件末尾,如果读取到的内容为空,那就是末尾了,其余的地方,即使看起来是孔,但其实有一个换行符的
print('num0 is:%d num1 is:%d num2 is %d'%(num0,num1,num2))
部分日志文件:
2020-12-01 05:40:18,128 [TRACE] recv:5c 53 00 00 00 7b ff 03 78 00 01 00 00 00 01 00 00 00 01 00 01 00 01 00 01 00 01 00 00 00 01 00 01 00 00 00 01 00 23 00 06 00 00 00 01 00 2a 00 04 00 00 00 02 00 05 00 08 00 00 00 02 00 21 00 05 00 00 00 01 00 29 00 00 00 00 00 02 00 09 00 05 00 00 00 01 00 1d 00 04 00 00 00 01 00 04 00 06 00 00 00 02 00 0c 00 04 00 00 00 00 00 19 00 02 00 00 00 02 00 22 00 03 00 00 00 02 00 03 00 03
2020-12-01 05:40:18,164 [TRACE] send:5d 53 00 00 00 06 ff 03 08 1a 00 01
2020-12-01 05:40:18,178 [TRACE] recv:5d 53 00 00 00 05 ff 03 02 00 00
2020-12-01 05:40:18,216 [TRACE] send:5e 53 00 00 00 06 ff 03 08 46 00 02
2020-12-01 05:40:18,227 [TRACE] recv:5e 53 00 00 00 07 ff 03 04 00 01 00 04
2020-12-01 05:40:18,267 [TRACE] send:5f 53 00 00 00 06 ff 03 08 50 00 30
2020-12-01 05:40:18,278 [TRACE] recv:5f 53 00 00 00 63 ff 03 60 00 00 3f a0 ae 14 3f a7 00 00 3f a0 ae 14 3f a7 00 00 3f a0 8f 5c 3f a2 00 00 3f a0 70 a4 3f 9d 8f 5c 3f a2 ae 14 3f a7 70 a4 3f 9d 8f 5c 3f a2 99 9a 41 c9 99 9a 41 c9 99 9a 41 c9 99 9a 41 c9 cc cd 41 c8 99 9a 41 c9 99 9a 41 c9 33 33 41 c7 99 9a 41 c9 cc cd 41 c8 99 9a 41 c9 cc cd 41 c8
2020-12-01 05:40:18,318 [TRACE] send:60 53 00 00 00 06 ff 03 00 32 00 01
2020-12-01 05:40:18,327 [TRACE] recv:60 53 00 00 00 05 ff 03 02 00 00
2020-12-01 05:40:18,369 [TRACE] send:61 53 00 00 00 06 ff 03 01 80 00 02
2020-12-01 05:40:18,378 [TRACE] recv:61 53 00 00 00 07 ff 03 04 66 66 41 ca
2020-12-01 05:40:18,420 [TRACE] send:62 53 00 00 00 06 ff 03 07 d7 00 03
2020-12-01 05:40:18,437 [TRACE] recv:62 53 00 00 00 09 ff 03 06 00 07 00 08 00 01
2020-12-01 05:40:18,472 [TRACE] send:63 53 00 00 00 06 ff 03 07 db 00 3c
2020-12-01 05:40:18,488 [TRACE] recv:63 53 00 00 00 7b ff 03 78 00 01 00 00 00 01 00 00 00 01 00 01 00 01 00 01 00 01 00 00 00 01 00 01 00 00 00 01 00 23 00 06 00 00 00 01 00 2a 00 04 00 00 00 02 00 05 00 08 00 00 00 02 00 21 00 05 00 00 00 01 00 29 00 00 00 00 00 02 00 09 00 05 00 00 00 01 00 1d 00 04 00 00 00 01 00 04 00 06 00 00 00 02 00 0c 00 04 00 00 00 00 00 19 00 02 00 00 00 02 00 22 00 03 00 00 00 02 00 03 00 03
2020-12-01 05:40:18,523 [TRACE] send:64 53 00 00 00 06 ff 03 08 1a 00 01
2020-12-01 05:40:18,537 [TRACE] recv:64 53 00 00 00 05 ff 03 02 00 00
2020-12-01 05:40:18,574 [TRACE] send:65 53 00 00 00 06 ff 03 08 46 00 02
2020-12-01 05:40:18,588 [TRACE] recv:65 53 00 00 00 07 ff 03 04 00 01 00 04
2020-12-01 05:40:18,625 [TRACE] send:66 53 00 00 00 06 ff 03 08 50 00 30
2020-12-01 05:40:18,638 [TRACE] recv:66 53 00 00 00 63 ff 03 60 00 00 3f a0 ae 14 3f a7 00 00 3f a0 ae 14 3f a7 00 00 3f a0 8f 5c 3f a2 00 00 3f a0 70 a4 3f 9d 8f 5c 3f a2 ae 14 3f a7 70 a4 3f 9d 8f 5c 3f a2 99 9a 41 c9 99 9a 41 c9 99 9a 41 c9 99 9a 41 c9 cc cd 41 c8 99 9a 41 c9 99 9a 41 c9 33 33 41 c7 99 9a 41 c9 cc cd 41 c8 99 9a 41 c9 cc cd 41 c8
2020-12-01 05:40:18,677 [TRACE] send:67 53 00 00 00 06 ff 03 00 32 00 01
2020-12-01 05:40:18,687 [TRACE] recv:67 53 00 00 00 05 ff 03 02 00 00
2020-12-01 05:40:18,728 [TRACE] send:68 53 00 00 00 06 ff 03 01 80 00 02
本文地址:https://blog.csdn.net/LCY133/article/details/110430825
上一篇: [IOS]新浪微博获取数据出错