在Python中,如何打印不包含特定字符串的行,而不是打印包含特定字符串的行:

19

我正在尝试压缩一个非常大的日志文件,为此,我必须消除每一行中包含字符串"StatusRequest"和"StatusResponse"的内容,同时打印不包含这个字符串的其他行。我目前的代码如下(可在命令提示符下运行):

```python with open('logfile.txt', 'r') as infile, open('condensed_logfile.txt', 'w') as outfile: for line in infile: if 'StatusRequest' not in line and 'StatusResponse' not in line: outfile.write(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()
你只需要关注脚本的末尾来回答我的问题,我真的不确定为什么这不起作用......它仍然输出每一行。而且我已经尝试交换not的位置,以使成语上更有意义,但它并没有改变代码的任何内容。非常感谢您的帮助 :)
4个回答

28

问题不在于你使用了not,而是or的意思并不是你想象的那样(如果你仔细考虑,它也不可能是):

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

你想知道表达式 ("StatusRequest" or "StatusResponse") 是否出现在 line 中。但是这个表达式实际上就等同于 "StatusRequest"

换句话说,你并不是在说“如果这两者都不存在于line中”。Python中没有 neithernone 函数,但它有一个 any 函数,所以你可以这样做:

if not any(value in line for value in ("StatusRequest", "StatusResponse")):
这不如英语那么简单。在英语中,你只需说“if none of the values 'StatusRequest' and 'StatusResponse' are in line”,但在 Python 中,你必须说“if none of the values coming up are in line, for values 'StatusRequest' and 'StatusResponse'”。
或者,在这种情况下更简单的方式是:
if "StatusRequest" not in line and "StatusResponse" not in line:

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


某些情况下,这是一个比我的解答更好的说明。 - TheSoundDefense
@abarnert 感谢您的详细解释。我唯一还有的问题是,如果文件开头有“StatusResponse”或“StatusRequest”,那么变量'result'中将没有任何值存储。我知道我最初没有提到这个问题,但任何帮助都受欢迎。我能否将结果变量初始化为[null],或者有更好的方法吗? - user3877194

6

请替换此行:

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

使用这个:

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

这并不是非常优雅,但它能解决问题。我不确定是否有更快的方法来比较两个字符串与同一行。


2

您需要单独放置每个条件:

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

1
< p > not 可用于否定括号内的表达式,就像您最初使用它时一样。您只需要修改它否定的内容,即字符串是否在 line 中找到:

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接