[英]In python, how can I print lines that do NOT contain a certain string, rather than print lines which DO contain a certain string:
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.我试图压缩一个非常大的日志文件,为此,我必须消除包含字符串“StatusRequest”和“StatusResponse”的每一行,同时打印不带此字符串的其他行。 The code I have so far is as follows (to run from the command prompt):
到目前为止,我的代码如下(从命令提示符运行):
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.
而且我已经尝试过切换 not 的位置,因此它在地道上更有意义,但它并没有改变代码的任何内容。 Any help is much appreciated :)
任何帮助深表感谢 :)
The problem isn't your use of not
, it's that or
doesn'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
.您问的是表达式
("StatusRequest" or "StatusResponse")
出现在line
。 But that expression is just the same thing as "StatusRequest"
.但该表达式与
"StatusRequest"
相同。
Put it in English: you're not trying to say "if neither of these is in line".用英语说:你不是想说“如果这些都不符合”。 Python doesn't have a
neither
/ none
function, but it does have an any
function, 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 in
and then negating the whole thing.) (另外,请注意您可以使用
not in
,而不是使用in
然后否定整个事情。)
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.
我不确定是否有更快的方法来将两个字符串与同一行进行比较。
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)
The not
can be used to negate the expression inside the parenthesis like you first had it. not
可用于否定括号内的表达式,就像您最初拥有它一样。 You just need to modify what it's negating, which is that the string is found within line
:您只需要修改它的否定内容,即在
line
找到该字符串:
if not ("StatusRequest" in line or "StatusResponse" in line):
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.