Filter out Bad Words From a Text File In Python

2k views Asked by At

I am wanting to read the contents of a text file and filter out the bad word from it. {UPDATED} So I updated my python file after what you told me but right now I am getting a no module error. When I run pip3 install profanity_filter it gives me a big error.enter image description here

I researched and found Profanity but it is not working

from profanity_filter import ProfanityFilter
pf = ProfanityFilter()

with open(input("Enter the name Of Your File"), "r") as myFile:
           j = myFile.read()
filtered = pf.censor(j)
print(filtered)

Here is the error I get when I try to run my file enter image description here

1

There are 1 answers

4
Barmar On

You need to create an instance of the class, and then call its censor() method.

And the argument has to be a string, not a list of strings. Call read() instead of readlines() to get the entire file as a single string.

from profanity_filter import ProfanityFilter
pf = ProfanityFilter()

with open(input("Enter the name Of Your File"), "r") as myFile:
   j = myFile.read()
filtered = pf.censor(j)
print(filtered)