What is replace_currency_symbols clean-text in Python?

In this shot, we will learn about the replace_currency_symbols in Python.

The replace_currency_symbols method is an in-built method, provided by the clean-text library in Python.

We can use it to clean data that has currency symbols in it.

We need to install it from pip in order to use it in our programs:

pip install clean-text

We can use the following syntax to use it.

Syntax

from cleantext import clean

clean(text, no_currency_symbols=True, replace_with_currency_symbol="")
  • clean is the function provided by the cleantext library.
  • To replace the currency symbols, we set the parameter no_currency_symbols to True.
  • Then, we provide the string with which to replace the currency symbols. If we don’t provide the string, the currency symbols will be replaced with <CUR> by default.

Let’s look at an example.

Code example

#import clean function
from cleantext import clean
#provide string with currency symbols
text = "$4000, 9000€, 100000¥"
#print text after replacing the currency symbols
print(clean(text, no_currency_symbols=True, replace_with_currency_symbol=""))

Code explanation

In the code written above:

  • In line 1, we import the clean function from the cleantext package.
  • In line 5, we provide the text that has currency symbols in it.
  • In line 8, we replace the currency symbols present in the text with empty space. When the parameter no_currency_symbols is set to True, the clean function will call the in-built replace_currency_symbols() function.

Free Resources