I have the following code:
import string
def translate_non_alphanumerics(to_translate, translate_to=\'_\'):
not_letters_or_digits = u\'!\"#%\\\'()*+,-./:
Instead of having to specify all the characters that need to be replaced, you could also view it the other way around and, instead, specify only the valid characters, like so:
import re
def replace_non_alphanumerics(source, replacement_character='_'):
result = re.sub("[^_a-zA-Z0-9]", replacement_character, source)
return result
This works with unicode as well as regular strings, and preserves the type (if both the replacement_character
and the source
are of the same type, obviously).