How to replace accented characters in python?

后端 未结 2 866
一生所求
一生所求 2020-12-28 18:19

My output looks like \'àéêöhello!\'. I need change my output like this \'aeeohello\', Just replacing the character à as a like this.

相关标签:
2条回答
  • 2020-12-28 18:40

    Hi Ganesh Please Use the below code.

    It Works For me!

    import unicodedata
    
    def strip_accents(text):
    
        try:
            text = unicode(text, 'utf-8')
        except NameError: # unicode is a default on python 3 
            pass
    
        text = unicodedata.normalize('NFD', text)\
               .encode('ascii', 'ignore')\
               .decode("utf-8")
    
        return str(text)
    
    s = strip_accents('àéêöhello')
    
    print s
    
    0 讨论(0)
  • 2020-12-28 18:40
    import unidecode
    somestring = "àéêöhello"
    
    #convert plain text to utf-8
    u = unicode(somestring, "utf-8")
    #convert utf-8 to normal text
    print unidecode.unidecode(u)
    

    output

    aeeohello

    0 讨论(0)
提交回复
热议问题