Python Censorship


/ Published in: Python
Save to your folder(s)

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.


Copy this code and paste it in your HTML
  1. bad_list = ["suck", "bad", "horrible", "crap"]
  2. sentence = "This bad could really suck as a horrible horrible. sentence"
  3.  
  4. def censor(sentence, bad_list):
  5. list_sentence = sentence.split(" ")
  6. symbols = "!@#$%^&*()_+{}|:<>?,./;'[]\=-\""
  7.  
  8. replaced_sentence = []
  9.  
  10. no_sym = ""
  11.  
  12. for word in list_sentence:
  13. checked = 0
  14. for bad_word in bad_list:
  15. replaced_word = ""
  16. one_time = True
  17.  
  18. if word == bad_word:
  19. replaced_word = "*" * len(word)
  20. checked += 1
  21. break
  22. else:
  23. if checked < 1:
  24. replaced_word = word
  25.  
  26. replaced_sentence.append(replaced_word)
  27.  
  28. replaced_sentence = " ".join(replaced_sentence)
  29.  
  30. return replaced_sentence
  31.  
  32. print censor(sentence, bad_list)

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.