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="")
cleanis the function provided by thecleantextlibrary.- To replace the currency symbols, we set the parameter
no_currency_symbolstoTrue. - 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 functionfrom cleantext import clean#provide string with currency symbolstext = "$4000, 9000€, 100000¥"#print text after replacing the currency symbolsprint(clean(text, no_currency_symbols=True, replace_with_currency_symbol=""))
Code explanation
In the code written above:
- In line 1, we import the
cleanfunction from thecleantextpackage. - In line 5, we provide the text that has currency symbols in it.
- In line 8, we replace the currency symbols present in the
textwith empty space. When the parameterno_currency_symbolsis set toTrue, thecleanfunction will call the in-builtreplace_currency_symbols()function.