Python Forum
Case Insensitive Censor Function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Case Insensitive Censor Function
#3
(Jan-14-2021, 08:38 PM)Larz60+ Wrote: you use i for index in both first and second loop, so the second loop overrides i of the first

It runs if you're lucky, but it is not going to deliver what you expect in the first loop.

you need to rename either the first or second 'i'

No, he doesn't. The first i is indeed in a loop:
    for i in banned:
but the second is just a test if that first i is present in the text
       if i in text:
The question is about preserving the original case in text.
I would use regular expressions with IGNORECASE in something like:
import re

def censor2(text, banned):
    for i in banned:
        replace = re.compile(re.escape(i), re.IGNORECASE)
        substitute = '*'*len(i)
        text = replace.sub(substitute, text)
    return text
Larz60+ likes this post
Reply


Messages In This Thread
Case Insensitive Censor Function - by HNiChuimin - Jan-14-2021, 12:10 PM
RE: Case Insensitive Censor Function - by Larz60+ - Jan-14-2021, 08:38 PM
RE: Case Insensitive Censor Function - by Serafim - Jan-14-2021, 11:13 PM
RE: Case Insensitive Censor Function - by Serafim - Jan-15-2021, 08:20 AM
RE: Case Insensitive Censor Function - by Serafim - Jan-16-2021, 12:13 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  List Censor Thread palmtrees 6 7,194 Oct-05-2016, 06:22 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020