忽略日志文件中时间戳的Pythonic脚本

2024-09-30 08:19:37 发布

您现在位置:Python中文网/ 问答频道 /正文

有两个日志文件:log Alog B。你知道吗

log A

2015-07-12 08:50:33,904 [Collection-3]INFO app -Executing Scheduled job: System: choppa1

2015-07-12 09:56:45,060 [Collection-3] INFO app - Executing Scheduled job: System: choppa1

2015-07-12 10:00:00,001 [Analytics_Worker-1] INFO  app  - Trigger for job AnBuildAuthorizationJob was fired.

2015-07-12 11:00:00,007 [Analytics_Worker-1] INFO app - Starting the AnBuildAuthorizationJob job.



log B

2014-07-12 09:50:33,904 [Collection-3] INFO  app  - Executing Scheduled job: System: choppa1

2014-07-12 09:56:45,060 [Collection-3] INFO  app  - Executing Scheduled job: System: choppa1

2014-07-12 10:00:00,001 [Analytics_Worker-1] INFO  app  - Trigger for job AnBuildAuthorizationJob was fired.

2014-07-12 10:00:00,007 [Analytics_Worker-1] INFO  app  - Starting the AnBuildAuthorizationJob job.

两个日志文件的内容相同,但时间戳不同。我需要通过忽略时间戳来比较这两个文件,即比较两个文件的每一行,即使它们有不同的时间戳,也不应该报告任何差异。我为此编写了以下python脚本:

#!/usr/bin/python
import re
import difflib

program = open("log1.txt", "r")
program_contents = program.readlines()
program.close() 

new_contents = []

pat = re.compile("^[^0-9]")

for line in program_contents:
 if re.search(pat, line):
  new_contents.append(line)

program = open("log2.txt", "r")
program_contents1 = program.readlines()
program.close() 

new_contents1 = []

pat = re.compile("^[^0-9]")

for line in program_contents1:
 if re.search(pat, line):
  new_contents1.append(line)

diff=difflib.ndiff(new_contents,new_contents1)
print(''.join(diff))

有没有更有效的方法来写上面的脚本??而且上面的脚本只有在timestamp位于行的开头时才起作用。我想写一个python脚本,即使时间戳在行的中间,它也应该可以工作。有人能帮我怎么做吗?你知道吗


Tags: 文件reinfologappnewlinecontents
2条回答

下面是一个小脚本,用于消除文件开头的时间戳。你知道吗

program = open("log1.txt", "r")
program_contents = program.readlines()
program.close()

program = open("log2.txt", "r")
program_contents1 = program.readlines()
program.close() 

for i in range(0,len(program_contents1)):
    if program_contents[i] == '\n':
        continue
    if program_contents[i][19:] == program_contents1[i][19:]:
        print("Matches")
I would  change pat = re.compile("^[^0-9]")

             to pat = re.compile("\d{4}-d{2}-d{2}

而且最好打开文件

                  with open(filename) as f:

这样python就可以为您关闭文件,不需要close(f)语句。你知道吗

相关问题 更多 >

    热门问题