In python, how can I print strings that DO NOT contain a specific string, and not print strings that contain some string:

I am trying to condense a very large log file, and for this I have to delete every line containing the string "StatusRequest" and "StatusResponse" when printing other lines without this line. The code that I still have is as follows (to run from the command line):

   if (sys.argv[1])=="--help":
       print ("\n")
       print ("Argument 1: Enter name of '.py' file")
       print ("-i or --input: name of Catalina log")
       print ("-o or --output: file to output to")
       print ("\n")
   if (sys.argv[1])=="-h":
       print ("\n")
       print ("Argument 1: Enter name of '.py' file")
       print ("-i or --input: name of Catalina log")
       print ("-o or --output: file to output to")
       print ("\n")

   else:
       print 'Number of arguments:', len(sys.argv), 'arguments.'
       print 'Argument List:', str(sys.argv)

       Numarg = (len(sys.argv))
       i=1
       while i<=(Numarg-4):
           search1="StatusRequest"
           search2="StatusResponse"
           if (sys.argv[Numarg-2])=="-o":
               outputfile=sys.argv[Numarg-1]

           if (sys.argv[Numarg-2])=="--output":
               outputfile=sys.argv[Numarg-1]

           if (sys.argv[i])=="-i":
               filename=(sys.argv[i+1])

               log=(filename)
               print ("You entered the log: " + log)

               f=open(log, 'r')
               read_data = f.read()
               f.close

               f=open(log, 'r')
               readlines_data=f.readlines()
               f.close()
               i=i+1
           if (sys.argv[i])=="--input":
               filename=(sys.argv[i+1])
               log=(filename)
               print ("You entered the log: " + log)

               f=open(log, 'r')
               read_data = f.read()
               f.close

               f=open(log, 'r')
               readlines_data=f.readlines()
               f.close()
               i=i+1
           for line in readlines_data:
               if not ("StatusRequest" or "StatusResponse") in line:
                   result=line
                   print (line)
       f=open(outputfile, 'a')
       f.write(result + "\n")
       f.close()

You can just focus on the end of the script to answer my question, really ... In any case, I'm not sure why this is not working ... It displays every line yet. And I already tried to switch the place not so that it makes sense idiomatically, but nothing changed with the code. Any help is much appreciated :)

+4
4

not, , or , , ( , , ):

if not ("StatusRequest" or "StatusResponse") in line:

, ("StatusRequest" or "StatusResponse") line. - , "StatusRequest".

: " ". Python neither/none, any, :

if not any(value in line for value in ("StatusRequest", "StatusResponse")):

, ; " " StatusRequest "" StatusResponse " ", Python " , , , " StatusRequest ", " StatusResponse".

, , :

if "StatusRequest" not in line and "StatusResponse" not in line:

( , not in in, .)

+9

:

if not ("StatusRequest" or "StatusResponse") in line:

:

if "StatusRequest" not in line and "StatusResponse" not in line:

, . , .

+1

:

for line in readlines_data:
    if ("StatusRequest" not in line) and "(StatusResponse" not in line):
        result = line
        print(line)
0

not , . , , , line:

if not ("StatusRequest" in line or "StatusResponse" in line):

0
source

All Articles