Replace non-ASCII characters with a single space

后端 未结 7 1437
死守一世寂寞
死守一世寂寞 2020-11-22 16:17

I need to replace all non-ASCII (\\x00-\\x7F) characters with a space. I\'m surprised that this is not dead-easy in Python, unless I\'m missing something. The following func

7条回答
  •  孤街浪徒
    2020-11-22 16:35

    For you the get the most alike representation of your original string I recommend the unidecode module:

    from unidecode import unidecode
    def remove_non_ascii(text):
        return unidecode(unicode(text, encoding = "utf-8"))
    

    Then you can use it in a string:

    remove_non_ascii("Ceñía")
    Cenia
    

提交回复
热议问题