👻 Check our latest review to choose the best laptop for Machine Learning engineers and Deep learning tasks!
I need to replace all non-ASCII (x00-x7F) characters with a space. I"m surprised that this is not dead-easy in Python, unless I"m missing something. The following function simply removes all non-ASCII characters:
def remove_non_ascii_1(text):
return "".join(i for i in text if ord(i)<128)
And this one replaces non-ASCII characters with the amount of spaces as per the amount of bytes in the character code point (i.e. the –
character is replaced with 3 spaces):
def remove_non_ascii_2(text):
return re.sub(r"[^x00-x7F]"," ", text)
How can I replace all non-ASCII characters with a single space?
Of the myriad of similar SO questions, none address character replacement as opposed to stripping, and additionally address all non-ascii characters not a specific character.
👻 Read also: what is the best laptop for engineering students?
We hope this article has helped you to resolve the problem. Apart from Replace non-ASCII characters with a single space, check other code Python module-related topics.
Want to excel in Python? See our review of the best Python online courses 2023. If you are interested in Data Science, check also how to learn programming in R.
By the way, this material is also available in other languages:
- Italiano Replace non-ASCII characters with a single space
- Deutsch Replace non-ASCII characters with a single space
- Français Replace non-ASCII characters with a single space
- Español Replace non-ASCII characters with a single space
- Türk Replace non-ASCII characters with a single space
- Русский Replace non-ASCII characters with a single space
- Português Replace non-ASCII characters with a single space
- Polski Replace non-ASCII characters with a single space
- Nederlandse Replace non-ASCII characters with a single space
- 中文 Replace non-ASCII characters with a single space
- 한국어 Replace non-ASCII characters with a single space
- 日本語 Replace non-ASCII characters with a single space
- हिन्दी Replace non-ASCII characters with a single space
Abu Dhabi | 2023-03-29
Thanks for explaining! I was stuck with Replace non-ASCII characters with a single space for some hours, finally got it done 🤗. I am just not quite sure it is the best method
Rome | 2023-03-29
Simply put and clear. Thank you for sharing. Replace non-ASCII characters with a single space and other issues with Regular Expressions was always my weak point 😁. I am just not quite sure it is the best method
California | 2023-03-29
Maybe there are another answers? What Replace non-ASCII characters with a single space exactly means?. Will use it in my bachelor thesis