使用python进行传输层的帧的类型判断

以下是按照一定规则,判断传输层的帧的种类的。

打开文件,按照行读取文件,然后进行写文件,注意,写入的是另外一个新创建的文件,不是原文件,这样也避免了对原文件的破坏。

使用字符串的split方法对其进行了处理。

#use the basic file and string operation.
import re

#str1 = '19:45:40:9854 Rx 1 0x7E0 s 8 02 10 03 00 00 00 00 00
 fdfdfdfdf'
fd1 = open('BUSMASTERLogFile_0.log', 'r')
fd1_new = open('BUSMASTERLogFile_0_new.log', 'w')
#str_list1 = fd1.readlines()
i = 0

try:
    while True:
        text_line = fd1.readline()
        if text_line:
            list1 = text_line.split(' s 8 ')
            if list1[1][0] == '0':
                print(str(i)+' single frame')
                text_line = text_line[:-2]+ ' SF' + '
' #add '
' is correct.
            elif list1[1][0] == '1':
                print(str(i)+' first frame')
                text_line = text_line[:-2]+ ' FF' + '
' #add '
' is correct.
            elif list1[1][0] == '2':
                print(str(i)+' consecutive frame')
                text_line = text_line[:-2]+ ' CF' + '
' #add '
' is correct.'
            elif list1[1][0] == '3':
                print(str(i)+' FlowControl')
                text_line = text_line[:-2]+ ' FC' + '
' #add '
' is correct.
            else:
                print(str(i)+' error!')
            i= i+1
            
            fd1_new.write(text_line)
        else:
            break
finally:
    fd1.close()
    fd1_new.close()
    
原文地址:https://www.cnblogs.com/praiseslow/p/13255864.html