【问题标题】:Python List is printing duplicates and cannot index valuesPython List 正在打印重复项并且无法索引值
【发布时间】:2021-07-20 12:54:21
【问题描述】:

我正在阅读一个巨大的日志文件并寻找特定的关键字。找到它们后,我需要打印具有 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

【问题讨论】:

  • 您有日志文件内容的示例吗?这真的有助于了解问题可能来自哪里。
  • 也许您只需要在print() 之后添加一个break,因为当您打印时,一个短语已经匹配,所以测试更多短语没有意义。
  • @Jonathon H,很遗憾,我无法共享包含机密数据的文件。
  • @quamrana 我试过了,它似乎有效,但我有一个条件可以进一步评估该列表是否不为空并向 MS Teams 发送通知警报,但它有点慢。
  • 听起来您已经准备好提出不同的问题了。

标签: python python-3.x list logging


【解决方案1】:

通过导入 fnmatch 更新了 sn-p

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 语句可能会做你想要的?如果这不是您的意思,可能需要进一步澄清。

更新了 sn-p(也许这是你的意图)

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']

【讨论】:

  • 如果这不是您的意思,您能否明确说明重复的含义。随意使用我的输入或输出,我只是为了更好地理解您的问题而伪造它
  • 这看起来真的很好@Jonathon H. :) 我一定会试试的。您能否再次查看我更新的问题,抱歉对使用 Stackoverflow 提问不太熟悉,因此我对其进行了修改,哈哈。
  • 这项工作就像一个魅力@Jonathon H。谢谢队友:)
  • 当然。总是很乐意帮助另一个 python 开发人员。另外,如果您很好奇,docs.python.org/3/howto/logging.html 是一个很棒的库,它在调试日志不显示在 prod 中的情况下做得很好。然后你可以从头开始写日志
  • 很棒的东西,我认为这个库会很有帮助。
猜你喜欢
  • 1970-01-01
  • 2013-11-05
  • 1970-01-01
  • 2022-01-05
  • 2020-12-26
  • 1970-01-01
  • 2018-02-25
  • 1970-01-01
  • 2015-05-28
相关资源
最近更新 更多