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.

Answer from Martijn Pieters on Stack Overflow
🌐
Reddit
reddit.com › r/learnpython › delete non-decodeable chars in string?
r/learnpython on Reddit: Delete non-decodeable chars in string?
March 7, 2024 -

Hello - i would like to delete all non-decodable chars from a string - and i tried it with the following code but his is not working -

s = "this  ง, ญ, ณ, น, ม, ร, ล, ฬ is a text ���}��j)���.ߪs*i� ��zmj��q��p with something between"
line = bytes(s, 'utf-8').decode('utf-8', 'ignore')
print(s)
print(line)

This ���}� chars are read from a pdf / doc / exe file or something like that and i would like to delete this information from ths string but keep everything else in the string (so the english, but also the thai-chars).

How can i do this and clean the string?

Discussions

python 2.7 - Removing Non Unicode characters from a file - Stack Overflow
I know this is repeated question but I have really tried hard all of the solutions so far. Can anyone please help how to get rid of chacracters like \xc3\xa2\xc2\x84\xc2\xa2 from a file? The file More on stackoverflow.com
🌐 stackoverflow.com
Remove unicode characters python - Stack Overflow
I am pulling tweets in python using tweepy. It gives the entire data in type unicode. Eg: print type(data) gives me It contains unicode characters in it. Eg: hello\u2026 im am fine\u2019s · I want to remove all of these unicode characters. Is there any regular expression i can use? str.replace isn't a viable option as unicode characters can be any values, from smileys to unicode apostrophes. ... All text is Unicode. Do you mean non... More on stackoverflow.com
🌐 stackoverflow.com
python - How can I remove non-ASCII characters but leave periods and spaces? - Stack Overflow
I'm working with a .txt file. I want a string of the text from the file with no non-ASCII characters. However, I want to leave spaces and periods. At present, I'm stripping those too. Here's the co... More on stackoverflow.com
🌐 stackoverflow.com
django - Deleting non unicode characters python - Stack Overflow
I think it's really difficult to ... of a unicode one I would suggest using the ascii codec for encoding instead of utf-8. ... If you want to remove the other characters, the encode function takes an optional second argument allowing you to ignore all characters that the specified codec can't handle: ... There is no such thing as a type-cast in Python, using str() ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Finxter
blog.finxter.com › home › learn python blog › 7 best ways to remove unicode characters in python
7 Best Ways to Remove Unicode Characters in Python - Be on the Right Side of Change
April 17, 2023 - One helpful technique is the unicodedata.normalize() method. This method can be used to convert accented characters to their non-accented counterparts, allowing us to remove diacritic marks.
🌐
Python Guides
pythonguides.com › remove-unicode-characters-in-python
How to Remove Unicode Characters in Python
September 3, 2025 - One of the simplest ways I remove unwanted Unicode characters is by encoding the string into ASCII and then decoding it back. ... # Method 1: Using encode() and decode() text = "Python is easy to learn 😊 but sometimes tricky ñ!" print("Original:", text) # Encode to ASCII and ignore errors ...
🌐
Medium
medium.com › @ryan_forrester_ › remove-special-characters-from-strings-in-python-complete-guide-53651c8163d9
Remove Special Characters from Strings in Python: Complete Guide | by ryan | Medium
January 7, 2025 - import unicodedata def clean_international_text(text): # Normalize Unicode characters normalized = unicodedata.normalize('NFKD', text) # Remove non-ASCII characters ascii_text = normalized.encode('ASCII', 'ignore').decode('ASCII') return ascii_text # Example with international text text = "Café München — スシ" clean_text = clean_international_text(text) print(clean_text) # Output: "Cafe Munchen "
🌐
InternetKatta
internetkatta.com › removal-of-non-ascii-characters-using-python
Removal of Non ASCII characters using Python
June 5, 2021 - One of the real scenarios I faced while calculating AWS signature before passing to API gateway and same matching with calculated signature by AWS is match and it throws an error because AWS signature calculation mechanism removes those characters and calculates signature but in your code you might not be doing then very straight it will not match. ... Below is Python script to remove those non ascii characters or junk characters.
🌐
Replit
replit.com › home › discover › how to remove unicode characters in python
How to remove Unicode characters in Python | Replit
April 13, 2026 - A leading ^ inside the brackets inverts the selection, so the pattern matches anything that is not an ASCII character. These matches are then replaced with an empty string, '', which deletes them from the text. Learn more about regular expressions in Python for advanced pattern matching. ... text = "Hello, 世界! Unicode symbols: ❤️ 😊 🐍" # Create a translation table that maps non-ASCII chars to None translation_table = {ord(char): None for char in text if ord(char) > 127} ascii_only = text.translate(translation_table) print(f"Original: {text}") print(f"ASCII only: {ascii_only}")
🌐
Bobby Hadz
bobbyhadz.com › blog › python-remove-non-ascii-characters-from-string
Remove non-ASCII characters from a string in Python | bobbyhadz
April 9, 2024 - Use the string.printable attribute to get a string of the ASCII characters. Use the filter() method to remove the non-ASCII characters.
Find elsewhere
🌐
YouTube
youtube.com › tsinfo technologies
Remove non-ASCII characters from a string using Python | Removing Non Ascii Characters in Python - YouTube
Welcome to our Python tutorial on removing non-ASCII characters from strings! In this video, we'll explore how to handle strings containing non-ASCII charact...
Published   February 14, 2024
Views   246
🌐
Python Guides
pythonguides.com › remove-non-ascii-characters-python
How Can Non-ASCII Characters Be Removed From A String In Python?
September 12, 2025 - The regex [^\x00-\x7F] matches all non-ASCII characters and removes them. This method is reliable and works when I want to strip everything outside of ASCII. Python 3.7 introduced the handy isascii() method in Python.
🌐
Python Forum
python-forum.io › thread-25728.html
How to Remove Non-ASCII Characters But Leave Line Breaks In Place?
I have a function in a Python script that serves to remove non-ASCII characters from strings before these strings are ultimately saved to an Oracle database. # This should remove any ASCII characters between 0-31 and also ones 127 & up. ...
🌐
Kite
kite.com › python › answers › how-to-remove-non-ascii-characters-in-python
Kite is saying farewell - Code Faster with Kite
November 20, 2022 - The largest issue is that state-of-the-art models don’t understand the structure of code, such as non-local context. We made some progress towards better models for code, but the problem is very engineering intensive.
🌐
Drumcoder
drumcoder.co.uk › blog › 2012 › jul › 13 › removing-non-ascii-chars-string-python
Removing non-ascii chars from a string in Python - drumcoder.co.uk
July 13, 2012 - I was processing some data from a database table, and the process was failing if a non-ascii character was passed. I didn't mind losing these characters, so needed a way to remove them from my string before processing.
🌐
Quora
quora.com › How-do-I-remove-non-ascii-characters-e-g-Ð-µ-Ž-ºÏƒ-¹-from-texts-in-pandas-dataframe-columns
How to remove non-ascii characters (e.g б§•¿µ´‡»Ž®ºÏƒ¶¹) from texts in pandas dataframe columns - Quora
Answer (1 of 2): I’m jumping to a conclusion here, that you don’t actually want to remove all characters with the high bit set, but that you want to make the text somewhat more readable for folks or systems who only understand ASCII. Maybe this assumption is wrong in which case just stop ...
🌐
Python Pool
pythonpool.com › home › blog › 5 solid ways to remove unicode characters in python
5 Solid Ways to Remove Unicode Characters in Python - Python Pool
June 14, 2021 - In this example, we will be using the ord() method and a for loop for removing the Unicode characters from the string. Ord() method accepts the string of length 1 as an argument and is used to return the Unicode code point representation of ...
🌐
Programming Idioms
programming-idioms.org › idiom › 147 › remove-all-non-ascii-characters › 2171 › python
Remove all non-ASCII characters, in Python
September 22, 2016 - Remove all non-digits characters · Remove all whitespace characters · Cheatsheets · Issues · Report a bug · × · Python · t = re.sub('[^\u0000-\u007f]', '', s) Python · t = sub(r'[^\x00-\x7f]', '', s) Python · t = '' for character in s: if ord(character) < 0x80: t += character ·