在python中,如何打印不包含某个字符串的行,而不是打印包含某个字符串的行:

use*_*194 9 python printing if-statement contains python-2.7

我试图压缩一个非常大的日志文件,为此,我必须删除包含字符串"StatusRequest"和"StatusResponse"的每一行,同时打印其他行没有这个字符串.到目前为止我的代码如下(从命令提示符运行):

   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()
Run Code Online (Sandbox Code Playgroud)

你可以专注于脚本的结尾来回答我的问题,真的......不管怎样,我不知道为什么这不起作用......它仍在输出每一行.而且我已经尝试切换不是这样的位置,这会更具意义,但它并没有改变任何代码.任何帮助深表感谢 :)

aba*_*ert 16

问题不是你的使用not,这or并不意味着你的想法(如果你认为它,它不能):

if not ("StatusRequest" or "StatusResponse") in line:
Run Code Online (Sandbox Code Playgroud)

你问的是表达式是否("StatusRequest" or "StatusResponse")出现在line.但那种表达方式与之相同"StatusRequest".

用英语说:你不是想说"如果这些都不符合要求".Python没有neither/ nonefunction,但它确实有一个any函数,所以你可以这样做:

if not any(value in line for value in ("StatusRequest", "StatusResponse")):
Run Code Online (Sandbox Code Playgroud)

这不如英语好; 在英语中,你可以说"如果没有值'StatusRequest'和'StatusResponse'在线",但在Python中,你必须说"如果没有出现的值符合,那么值'StatusRequest'和'StatusResponse'".

或者,在这种情况下可能更简单:

if "StatusRequest" not in line and "StatusResponse" not in line:
Run Code Online (Sandbox Code Playgroud)

(另外,请注意您可以使用not in,而不是使用in然后否定整个事物.)


The*_*nse 6

替换这一行:

if not ("StatusRequest" or "StatusResponse") in line:
Run Code Online (Sandbox Code Playgroud)

有了这个:

if "StatusRequest" not in line and "StatusResponse" not in line:
Run Code Online (Sandbox Code Playgroud)

不是超级优雅,但它会起到作用。我不确定是否有更快的方法来将两个字符串与同一行进行比较。