繁体   English   中英

Python List 正在打印重复项并且无法索引值

[英]Python List is printing duplicates and cannot index values

我正在读取一个巨大的日志文件并寻找特定的关键字。 找到它们后,我需要打印具有 found 关键字的每一行。 目前,我的代码会打印与我的列表中找到的行一样多的重复行。 下面是我的代码。 这是我所做的更新和修改的完整代码,基本上在这里我发现它发送带有关键字错误的行,带有结束行并且有点慢,我是否可以只打印第一行使用索引:

important = []
phrases = ['Error']

with open("mypath\\to\\my\\logfile.log") as file:
    line = file.readline()
    for line in file:
        for phrase in phrases:
            if phrase in line:
                important.append(line)
                pattern = 'ERROR*' or 'Warning*'
                matching = fnmatch.filter(important, pattern)
                myTeamsMessage = pymsteams.connectorcard("myTeamsWebhook")
                if len(important) != 0:
                    # Add text to the message.
                    myTeamsMessage.text(str(matching))
                    # send the message.
                    myTeamsMessage.send()
                    # print(matching)
                    break
                else:
                    # Add text to the message.
                    myTeamsMessage.text("Log has no error, proceed with deployment")
                    # send the message.
                    myTeamsMessage.send()
                break

导入 fnmatch 的更新片段

import fnmatch
#important lines?
important = []
phrases = ['Null-Reference-Exception']

with open("logfile.log") as file:
  line = file.readline()
  for line in file:
    for phrase in phrases:
      #print("Line", line)
      #print("phrase", phrase)
      if phrase in line:
        important.append(line)
        pattern = 'ERROR*' or 'Warning*'
        #https://docs.python.org/3/library/fnmatch.html
        matching = fnmatch.filter(important, pattern)
        print(str(matching))

我的示例日志文件内容:

WARNING: You forgot to initialize a variable on line 27
ERROR: This code broke unexpectedly Null-Reference-Exception 34
ERROR: This other code broke unexpectedly Null-Reference-Exception line 290

这个例子的输出

['ERROR: This code broke unexpectedly Null-Reference-Exception 34\n']
['ERROR: This code broke unexpectedly Null-Reference-Exception 34\n', 'ERROR: This other code broke unexpectedly Null-Reference-Exception line 290']

您的意思是上面输出中的第二个列表是重复的吗? 您当前的代码添加重要的行,然后过滤任何与打印模式匹配的内容。 也许 if 语句可以满足您的需求? 如果这不是您的意思,可能需要更多说明。

更新的片段(也许这是您的意图)

import fnmatch
#important lines?
important = []
phrases = ['Null-Reference-Exception']
matches = []
with open("logfile.log") as file:
  line = file.readline()
  for line in file:
    for phrase in phrases:
      #print("Line", line)
      #print("phrase", phrase)
      if phrase in line:
        important.append(line)
        pattern = 'ERROR*' or 'Warning*'
        #https://docs.python.org/3/library/fnmatch.html
        matching = fnmatch.filter(important, pattern)
        for m in matching:
          if m not in matches:
            matches.append(m)
print(str(matches))

我使用了与上面相同的输入

新输出

['ERROR: This code broke unexpectedly Null-Reference-Exception 34\n', 'ERROR: This other code broke unexpectedly Null-Reference-Exception line 290']

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM