What is replace_emails() method of clean-text package in Python?

clean-text package

clean-text is a third-party package that is used to pre-process text data to obtain a normalized text representation.

The package can be installed via pip. Check the following command to install the clean-text package:

pip install clean-text

replace_emails() method

The replace_emails() method is used to replace all emails in the given text with the replacement string.

Method signature

replace_emails(text, replace_with="<EMAIL>")

Parameters

  • text: This is the text data.
  • replace_with: This is the replacement string.

Return value

The method returns the text data where all the emails are replaced by the replacement string.

Code

import cleantext
string = """hello educative - fahim@educative.io - hello edpresso"""
new_string = cleantext.replace_emails(string, replace_with="fahim_mail")
print("Original String - '" + string + "'")
print("Modified String - '" + new_string + "'")

Code explanation

  • Line 1: The cleantext package is imported.
  • Line 3: We define a string with an email in it.
  • Line 5: We use the replace_urls() method to replace the email in the string with fahim_mail.
  • Lines 7–8: We print the original and modified string.

Free Resources