简体   繁体   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:

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.

相关问题 如何在 Python 中打印包含某个字符串的文本文件的行? - How to print the lines of a text file that contain a certain string in Python? 使用python从文本文件中删除不包含某些字符串的行 - Remove lines from a text file which do not contain a certain string with python 删除包含特定字符串的行 - Remove lines that contain certain string 在python中找到某个字符串后如何打印所有行? - How to print all the lines after it found certain string in python? 如何仅打印包含子字符串的行 - how to print only lines which contain a substring Python,将文件中具有特定字符串的所有行添加到列表中,然后随机选择要打印的字符串? - Python, Adding all lines with a certain string from a file to list then randomly choosing which string to print? Python 计算字符串的出现次数并打印包含它们的行,以及打印字符串出现的次数,带有多个子句 - Python count occurences of a string and print the lines that contain them, as well as print number of occurences of string, with multiple clauses 如果字符串不包含 Python 中的某些字符,我如何从列表中删除它 - How do i remove a string from a list if it DOES NOT contain certain characters in Python 如何从字符串中某些行的末尾删除 4 个字符? - How do I delete 4 characters from the end of certain lines in a string? 如何只打印包含某个字符串的文件中的行? - How to only print lines from a file containing a certain string?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM