| 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) |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| 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) |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战