0

I presently have code that deletes all lines from a text file that contain one specific string. Here it is:

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")    

My question is: how can I include other strings? In other words, I want to tell the script that if a line contains "string1" or some other string "string2", then delete that line. I know I could just repeat the code I put above for every such string, but I'm certain there's some shorter and more efficient way to write that.
Many thanks in advance!

RealFL
  • 117
  • 4
  • 11
  • This might help: https://stackoverflow.com/questions/6531482/how-to-check-if-a-string-contains-an-element-from-a-list-in-python – yinnonsanders Jul 07 '17 at 18:29

4 Answers4

2

Just abstract it out into a function and use that?

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)      
havanagrawal
  • 1,039
  • 6
  • 12
1

might be good to have a function

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): 
PYA
  • 8,096
  • 3
  • 21
  • 38
0
if "string1" in line or "string2" in line:

This should work I think

J0hn
  • 570
  • 4
  • 19
  • Updated, yeah I tried running and it only worked this way. Depending on how many strings OP has to check, some of the other methods people have posted involving lists might be better. – J0hn Jul 07 '17 at 18:33
0

You can loop through a list of your blacklisted strings while keeping track of if one of the blacklisted strings was present like this:

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")  
jacoblaw
  • 1,263
  • 9
  • 9