怪奇物语

怪奇物语

首页 新随笔 联系 管理
import re


def read_log(filePath):
    log = {}
    timeList = []
    contentList = []
    lastTime = None
    timeReg = r'((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})\/(((0[13578]|1[02])\/(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)\/(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))-02-29))\s+([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])'
    pattern = re.compile(timeReg)
    with open(filePath, 'r', encoding='utf-8') as f:
        lines = f.readlines()
        for line in lines:
            contentList.append(line)
            res = re.search(pattern, line)
            if res:
                currentTime = res.group()
                if lastTime is None:
                    lastTime = currentTime
                if lastTime != currentTime and lastTime is not None:
                    contentList.pop()
                    log[lastTime] = contentList
                    contentList.clear()
                    contentList.append(line)
                    lastTime = currentTime
    print('-------------')
    print(log)


if __name__ == '__main__':
    filePath = 'G:\EMA_v2\TerminalMessage\TerminalMessage.log-20221109-1.log'
    read_log(filePath)
    # res = {
    #     '2022/11/09 04:01:56':
    #         [
    #             "2022/11/09 13:17:12.922  The Command Completed Didn't Respond Within[180]Seconds.(13:15~13:12)\n",
    #             '请工程值班处理,Robot在[180]秒内,未及时回复动作结果给CPC,检查Robot状况\n',
    #             '若发生莫停,需手动切换CPC的Auto/Manual Mode [Repeat 5]\n',
    #             '2022/11/09 13:17:12.922  Robot Action1 Command Result Is NG\n',
    #             '2022/11/09 13:17:12.922  ANMOR100 Robot做动失败,确认Robot状况后做原点复归\n'],
    #     '2022/11/09 13:15:12':
    #         [
    #             "2022/11/09 13:17:12.922  The Command Completed Didn't Respond Within[180]Seconds.(13:15~13:12)\n",
    #             '请工程值班处理,Robot在[180]秒内,未及时回复动作结果给CPC,检查Robot状况\n',
    #             '若发生莫停,需手动切换CPC的Auto/Manual Mode [Repeat 5]\n',
    #             '2022/11/09 13:17:12.922  Robot Action1 Command Result Is NG\n',
    #             '2022/11/09 13:17:12.922  ANMOR100 Robot做动失败,确认Robot状况后做原点复归\n']
    # }


2022/11/09 04:01:56.533  [S2F21] Port [1] 被Host退卡 : Host Cancel : AUOpVerify NG[TE36408]:Equipment doe not match with equipment capability.
2022/11/09 13:15:12.266  The Command Completed Didn't Respond Within[180]Seconds.(13:15~13:12)
2022/11/09 13:15:12.266  请工程值班处理,Robot在[180]秒内,未及时回复动作结果给CPC,检查Robot状况
2022/11/09 13:15:12.266  若发生莫停,需手动切换CPC的Auto/Manual Mode
2022/11/09 13:17:12.922  The Command Completed Didn't Respond Within[180]Seconds.(13:15~13:12)
请工程值班处理,Robot在[180]秒内,未及时回复动作结果给CPC,检查Robot状况
若发生莫停,需手动切换CPC的Auto/Manual Mode [Repeat 5]
2022/11/09 13:17:12.922  Robot Action1 Command Result Is NG
2022/11/09 13:17:12.922  ANMOR100 Robot做动失败,确认Robot状况后做原点复归

修改之后的代码

import re


def read_log(file_path):
    log = {}
    time_list = []
    content_list = []
    last_time = None
    time_reg = r'((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})\/(((0[13578]|1[02])\/(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)\/(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))-02-29))\s+([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])'
    pattern = re.compile(time_reg)
    with open(file_path, 'r', encoding='utf-8') as f:
        lines = f.readlines()
        for line in lines:
            content_list.append(line)
            res = re.search(pattern, line)
            if res:
                cuttent_time = res.group()
                if last_time is None:
                    last_time = cuttent_time
                    continue
                if last_time != cuttent_time and last_time is not None:
                    content_list.pop()
                    log[last_time] = content_list.copy()
                    content_list.clear()
                    content_list.append(line)
                    last_time = cuttent_time
        log[last_time] = content_list.copy()
    return log


if __name__ == '__main__':
    filePath = 'test.txt'
    log = read_log(filePath)
    print(log)
posted on 2022-11-11 19:58  超级无敌美少男战士  阅读(57)  评论(0)    收藏  举报