【问题标题】:In python, how can I print lines that do NOT contain a certain string, rather than print lines which DO contain a certain string:在 python 中,如何打印不包含某个字符串的行,而不是打印包含某个字符串的行:
【发布时间】:2014-09-22 13:46:32
【问题描述】:

我正在尝试压缩一个非常大的日志文件,为此,我必须消除包含字符串“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()

您可以只关注脚本的结尾来回答我的问题,真的...无论如何,我不确定为什么这不起作用...它仍在输出每一行。而且我已经尝试过切换 not 的位置,这样在惯用上会更有意义,但它并没有改变代码的任何内容。非常感谢任何帮助:)

【问题讨论】:

    标签: python python-2.7 if-statement printing contains


    【解决方案1】:

    问题不在于你使用了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'和“状态响应””。

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

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

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

    【讨论】:

    • 肯定比我的回答更好的解释。
    • @abarnert 感谢您的详尽解释。我唯一的另一个问题是,如果文件开头有“StatusResponse”或“StatusRequest”,那么变量“result”中将没有值存储。我知道我最初没有解决这个问题,但欢迎任何帮助。我可以将结果变量初始化为 [null] 还是有更好的方法来做到这一点?
    【解决方案2】:

    替换这一行:

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

    有了这个:

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

    不是很优雅,但它可以解决问题。我不确定是否有更快的方法将两个字符串与同一行进行比较。

    【讨论】:

      【解决方案3】:

      您必须分别放置每个条件:

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

      【讨论】:

        【解决方案4】:

        not 可用于否定括号内的表达式,就像您第一次使用它一样。你只需要修改它的否定,即字符串在line中找到:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2010-10-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-12-23
          • 1970-01-01
          相关资源
          最近更新 更多