Last active
May 19, 2022 22:21
-
-
Save kgriffs/8c3e6e54f603719dc1a1d3c403661f58 to your computer and use it in GitHub Desktop.
Strip non-printable characters in a Python string using a regex
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Should match all non-printables except Unicode whitespace characters | |
r = re.compile(r'[^\w\s' + re.escape(string.punctuation) + ']') | |
def clean(text): | |
return r.sub('', text).strip() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment