0

I have a list of bad words and another list of sentences. I want to list all the sentences that have at least one bad word in newlines list then I want to delete it so I have pure sentences and I try like this but not work

f = open("demofile.txt")
read1= f.readlines()
g = open("abuse dic.txt")
read2 = g.readlines()
newlines = []
for line in f:
    if any(s in line for s in read2):
        newlines.append(line)
print(newlines)
g.close()
f.close()
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437

1 Answers1

1

Your code

if any(s in line for s in read2):
    newlines.append(line)

simply appends the line as is. You need to repeatedly remove all bad words from line before you append it to newlines. This answer shows a good way to do so.

rajah9
  • 11,645
  • 5
  • 44
  • 57