Skip to content

Instantly share code, notes, and snippets.

@kgriffs
Last active May 19, 2022 22:21
Show Gist options
  • Save kgriffs/8c3e6e54f603719dc1a1d3c403661f58 to your computer and use it in GitHub Desktop.
Save kgriffs/8c3e6e54f603719dc1a1d3c403661f58 to your computer and use it in GitHub Desktop.
Strip non-printable characters in a Python string using a regex
# 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