Sometimes, we want to replace non-ASCII characters with a single space in Python.
In this article, we’ll look at how to replace non-ASCII characters with a single space in Python.
How to replace non-ASCII characters with a single space in Python?
To replace non-ASCII characters with a single space in Python, we can use the unidecode module.
To install it, we run:
pip install unidecode
Then we use it by writing:
from unidecode import unidecode
def remove_non_ascii(text):
return unidecode(text)
n = remove_non_ascii(u"Ceñía")
print(n)
We have the remove_non_ascii function that takes the text string.
Then we call unideocde with text to return an ASCII string.
Next, we call remove_non_ascii function with the u"Ceñía" unicode string.
Therefore, n is 'Cenia'.
Conclusion
To replace non-ASCII characters with a single space in Python, we can use the unidecode module.