TransWikia.com

Filter out Bad Words From a Text File In Python

Stack Overflow Asked on February 1, 2021

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

One Answer

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)

Answered by Barmar on February 1, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP