Replace non-ASCII characters with a single space
ascii, encoding, python, unicode
Solution
Your `''.join()` expression is filtering, removing anything non-ASCII; you could use a conditional expression instead:
return ''.join([i if ord(i) < 128 else ' ' for i in text])
This handles characters one by one and would still use one space per character replaced.
Your regular expression should just replace consecutive non-ASCII characters with a space:
re.sub(r'[^\x00-\x7F]+',' ', text)
Note the `+` there.
Problem
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.
Related problems
- Removing unicode \u2026 like characters in a string in python2.7
- NameError: global name 'unicode' is not defined - in Python 3
- efficiently replace bad characters
- List comprehension without [ ] in Python
- How can I remove non-ASCII characters but leave periods and spaces?
- How to make the python interpreter correctly handle non-ASCII characters in string operations?
- Stripping non printable characters from a string in python
- Python - Unicode to ASCII conversion