Ethan San Jose
Ethan San Jose

Reputation: 27

Letter & Word replacing in Python

I'm trying to read a txt file and have certain words & letters changed into the letter "X" (as a way of censoring certain words found in the email) I've tried:

email_one = open("email_one.txt", "r").read()

def email1(email):
  for words in email:
    if words == "learning algorithms":
      return words.replace("learning algorithms", "X")

This is my code though I cannot get it to work, it's supposed to read the email and turn all instances of "learning algorithms" into the letter "X"

I have also searched in similar questions but none of the answers there have been able to help me solve this simple thing...

Upvotes: 0

Views: 91

Answers (2)

tobias_k
tobias_k

Reputation: 82899

There are several problems with your code:

  • email is a string and you are iterating characters, so words can never be "learning algorithms"
  • you are replacing in words, not in email
  • you are returning after the first replacement (and not at all if there is no match)

Instead, I suggest using a regular expression and re.sub to replace all the bad words:

import re

email = """I'm trying to read a txt file and have certain words & letters
changed into the letter "X" (as a way of censoring certain words found in
the email) I've tried:"""
bad_words = ["certain words", "letter"]
pattern = r"\b(" + "|".join(bad_words) + r")\b"
censored = re.sub(pattern, lambda m: "X" * len(m.group()), email)
print(censored)

Here, the second parameter to re.sub is a callback, replacing the bad word with a number of X matching the length of the word. If you want a single X, just replace the lambda function with "X". Also, the \b is a word-boundary, which means that the bad word can not be a part of another word (thus letter is replaced, but letters is not). Output:

I'm trying to read a txt file and have XXXXXXXXXXXXX & letters
changed into the XXXXXX "X" (as a way of censoring XXXXXXXXXXXXX found in
the email) I've tried:

Upvotes: 1

Glatinis
Glatinis

Reputation: 347

This is very simple, It's not working because "learning algorithms" is two words, but you are using a for loop which is breaking the string down to the letter.

email_one = open("email_one.txt", "r").read()
print(email_one)

def email1(email):
  return email.replace("learning algorithms", "X")

print(email1(email_one))

Upvotes: 1

Related Questions