I'm trying to read a text from a text file, read lines, delete lines that contain specific string (in this case 'bad' and 'naughty'). The code I wrote goes like this:
infile = file('./oldfile.txt')
newopen = open('./newfile.txt', 'w')
for line in infile :
if 'bad' in line:
line = line.replace('.' , '')
if 'naughty' in line:
line = line.replace('.', '')
else:
newopen.write(line)
newopen.close()
I wrote like this but it doesn't work out.
One thing important is, if the content of the text was like this:
good baby
bad boy
good boy
normal boy
I don't want the output to have empty lines. so not like:
good baby
good boy
normal boy
but like this:
good baby
good boy
normal boy
What should I edit from my code on the above?
Use python-textops package :
You could simply not include the line into the new file instead of doing replace.
You can make your code simpler and more readable like this
using a Context Manager and any.
Today I needed to accomplish a similar task so I wrote up a gist to accomplish the task based on some research I did. I hope that someone will find this useful!
Link to Gist: emailStripper.py
Best, Az
I have used this to remove unwanted words from text files:
Or to do the same for all files in a directory:
I'm sure there must be a more elegant way to do it, but this did what I wanted it to.