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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25044938/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 19:38:19  来源:igfitidea点击:

In python, how can I print lines that do NOT contain a certain string, rather than print lines which DO contain a certain string:

pythonpython-2.7if-statementprintingcontains

提问by user3877194

I am trying to condense a very large log file, and to do so, I must eliminate every line which contains the string "StatusRequest" and "StatusResponse", while printing the other lines w/o this string. The code I have so far is as follows (to run from the command prompt):

我试图压缩一个非常大的日志文件,为此,我必须消除包含字符串“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()
   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...Anyways, I am not sure why this doesn't work...It is outputting every line still. And I already tried switching the place of the not so it would make more sense idiomatically, but it didn't change anything with the code. Any help is much appreciated :)

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

回答by TheSoundDefense

Replace this line:

替换这一行:

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

With this one:

有了这个:

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

Not super elegant, but it will do the trick. I'm not sure if there's a faster way to compare two strings against the same line.

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

回答by Christian

You have to put each condition separately:

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

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

回答by abarnert

The problem isn't your use of not, it's that ordoesn't mean what you think it does (and if you think it through, it couldn't):

问题不在于您对 的使用not,这or并不意味着您认为它的作用(如果您仔细考虑,它就不能):

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

You're asking whether the expression ("StatusRequest" or "StatusResponse")appears in line. But that expression is just the same thing as "StatusRequest".

您在问表达式是否("StatusRequest" or "StatusResponse")出现在line. 但是那个表达式和"StatusRequest".

Put it in English: you're not trying to say "if neither of these is in line". Python doesn't have a neither/nonefunction, but it does have an anyfunction, so you can do this:

用英语说:你不是想说“如果这些都不符合”。Python 没有neither/none函数,但它有一个any函数,所以你可以这样做:

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

This isn't quite as nice as English; in English, you can just say "if none of the values 'StatusRequest' and 'StatusResponse' are in line", but in Python, you have to say "if none of the values coming up are in line, for values 'StatusRequest' and 'StatusResponse'".

这不如英语好;在英语中,您可以只说“如果没有值 'StatusRequest' 和 'StatusResponse' 符合要求”,但在 Python 中,您必须说“如果没有出现的值符合要求,对于值 'StatusRequest'和'状态响应'”。

Or, maybe more simply in this case:

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

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

(Also, notice that you can use not in, instead of using inand then negating the whole thing.)

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

回答by stever

The notcan be used to negate the expression inside the parenthesis like you first had it. You just need to modify what it's negating, which is that the string is found within line:

not可用于否定括号内的表达式像你先过它。您只需要修改它的否定内容,即在以下位置找到字符串line

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

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