简体   繁体   中英

Python List is printing duplicates and cannot index values

I am reading from a huge log file and looking for particular keywords. When they are found, I need to print each line that has the found keyword. Currently my code prints duplicate lines as many as the lines found from my list. Here is my code below. This is the updated and modified full code of what I did, basically here I found that it sends the lines with the keyword error, with an end line and is a bit slow, is it possible for me to just only print the first line using indexing:

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

updated snippet with import of 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))

my example logfile contents:

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

output with this example

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

Do you mean the second list in output above being the duplicate? Your current code adds important lines and then filters any that match pattern for the print. Perhaps an if statement might do what you want? Might need more clarification if this is not what you meant.

Updated snippet (perhaps this is your intent)

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))

I used the same input as above

New Output

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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