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!

1

4 Answers 4

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)      
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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

Comments

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

This should work I think

1 Comment

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.