如果行包含少数指定字符串Python之一,则从文本文件中删除行

时间:2017-07-07 18:25:33

标签: python python-3.x if-statement

我目前有代码删除包含一个特定字符串的文本文件中的所有行。这是:

import os  
with open(r"oldfile") as f, open(r"workfile", "w") as working:    
    for line in f:   
       if "string1" not in line:  
           working.write(line)  
os.remove(r"oldfile")  
os.rename(r"workfile", r"oldfile")    

我的问题是:如何包含其他字符串?换句话说,我想告诉脚本,如果一行包含“string1”某些其他字符串“string2”,则删除该行。我知道我可以重复上面为每个这样的字符串添加的代码,但我确信有一些更短更有效的方法来编写它。
非常感谢提前!

4 个答案:

答案 0 :(得分:2)

将它抽象成一个函数并使用它?

def should_remove_line(line, stop_words):
    return any([word in line for word in stop_words])

stop_words = ["string1", "string2"]
with open(r"oldfile") as f, open(r"workfile", "w") as working:    
for line in f:   
   if not should_remove_line(line, stop_words):  
       working.write(line)      

答案 1 :(得分:1)

可能有一个功能

def contains(list_of_strings_to_check,line):
  for string in list_of_strings_to_check:
    if string in line:
      return False
  return True

list_of_strings = ["string1","string2",...]
...
for line in f:   
       if contains(list_of_strings,line): 

答案 2 :(得分:0)

if "string1" in line or "string2" in line:

我觉得这应该有用

答案 3 :(得分:0)

您可以循环浏览列入黑名单的字符串列表,同时跟踪其中一个列入黑名单的字符串是否存在:

import os  
blacklist = ["string1", "string2"]
with open(r"oldfile") as f, open(r"workfile", "w") as working:    
    for line in f:   
        write = True
        for string in blacklist:
           if string in line:  
               write = False
               break
        if write:
               working.write(line) 
os.remove(r"oldfile")  
os.rename(r"workfile", r"oldfile")