繁体   English   中英

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

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

我试图压缩一个非常大的日志文件,为此,我必须消除包含字符串“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 的位置,因此它在地道上更有意义,但它并没有改变代码的任何内容。 任何帮助深表感谢 :)

问题不在于你的使用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然后否定整个事情。)

替换这一行:

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

有了这个:

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

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

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

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

not可用于否定括号内的表达式,就像您最初拥有它一样。 您只需要修改它的否定内容,即在line找到该字符串:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM