python如何将法语字符àéêöhello转换成英文字符aeeohello

6次阅读

共计 345 个字符,预计需要花费 1 分钟才能阅读完成。

今天遇到一个需求,需要对表格数据进行数据清洗,其中有的字符是法语字符。就是这类àéêö,和咱们的注音差不多。我们的目标是将这类法语字符转换成英文字符。

我们需要用到 unidecode,如下面代码

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