/ Published in: Python
A simple Python censor script that takes 2 arguments. One is for the text you want to censor and the other is the list of words that should not be viewed.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
bad_list = ["suck", "bad", "horrible", "crap"] sentence = "This bad could really suck as a horrible horrible. sentence" def censor(sentence, bad_list): list_sentence = sentence.split(" ") symbols = "!@#$%^&*()_+{}|:<>?,./;'[]\=-\"" replaced_sentence = [] no_sym = "" for word in list_sentence: checked = 0 for bad_word in bad_list: replaced_word = "" one_time = True if word == bad_word: replaced_word = "*" * len(word) checked += 1 break else: if checked < 1: replaced_word = word replaced_sentence.append(replaced_word) replaced_sentence = " ".join(replaced_sentence) return replaced_sentence print censor(sentence, bad_list)