Sometimes, we want to remove non-ASCII characters but leave periods and spaces with Python.
In this article, we’ll look at how to remove non-ASCII characters but leave periods and spaces with Python.
How to remove non-ASCII characters but leave periods and spaces with Python?
To remove non-ASCII characters but leave periods and spaces with Python, we can get a list of printable characters with string.printable
and use that to filter out the unwanted characters.
For instance, we write
import string
s = "some\x00string. with\x15 funny characters"
printable = set(string.printable)
filtered = filter(lambda x: x in printable, s)
to create a set from string.printable
with set
.
Then we call filter
with a function that returns if character x
in string s
is in the printable
set and the s
string.
Then we get an iterable with the characters that are in printable
in s
.
Conclusion
To remove non-ASCII characters but leave periods and spaces with Python, we can get a list of printable characters with string.printable
and use that to filter out the unwanted characters.