How do I get str.translate to work with Unicode strings?

前端 未结 7 908
孤城傲影
孤城傲影 2020-12-01 00:17

I have the following code:

import string
def translate_non_alphanumerics(to_translate, translate_to=\'_\'):
    not_letters_or_digits = u\'!\"#%\\\'()*+,-./:         


        
7条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-01 00:41

    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).

提交回复
热议问题