I am creating a program to edit a text file using the fileinput
library. What I want to do is search every line in the text file for a certain string and delete it. But I also want to delete the line above if it contains a different but defined string.
For example, if I want to delete every line that contains the word "dog" but I also want to the delete the line above if it contains the word "cat".
The original text would be:
I enjoy animals.
I have 2 fish.
I also have a cat.
I also have a dog.
I do not have a gerbil.
My dog stays inside.
The fish have a tank.
The cat goes outside.
My animals are awesome.
The result I want to be:
I enjoy animals.
I have 2 fish.
I do not have a gerbil.
The fish have a tank.
The cat goes outside.
My animals are awesome.
So I am deleting all the lines with "dog" and also the line with "cat" that is above the line with "dog" but not the line with "cat" that doesn't have a line with "dog" after it.
Any thoughts or suggestions?
CodePudding user response:
def dog(line):
if "dog" in line:
return True
else:
return False
def cat(line):
if "cat" in line:
return True
else:
return False
saved_lines=[]
last_line=""
for line in lines:
if dog(line) is False: # Not a dog line
saved_lines.append(l)
else:
if cat(last_line) is True:
saved_lines=saved_lines[:-1] # Delete the last line from the saved list
last_line = line # At end of loop, refresh last_line to value of current line
Rather than performing a delete, here the code selectively copies content into saved_lines
. If a line has a "dog" then it wont be copied. If a line does have a "dog", then the last line saved is a candidate for removal, on detection of a "cat".
Your content should be saved in the saved_lines
list variable.
The downside of this method is if your starting list is huge, then you're very nearly doubling it by saving a selective copy. But chances are you'll be ok.
CodePudding user response:
Read the file and store it in a list and do this, and write back to another file.
data = ['I enjoy animals.',
'I have 2 fish.',
'I also have a cat.',
'I also have a dog.',
'I do not have a gerbil.',
'My dog stays inside.',
'The fish have a tank.',
'The cat goes outside.',
'My animals are awesome.']
word_list = ['cat', 'dog']
result = [item for item in data if not any(word in item for word in word_list)]
# result
['I enjoy animals.',
'I have 2 fish.',
'I do not have a gerbil.',
'The fish have a tank.',
'My animals are awesome.']