如何替换python中的重音字符? [英] How to replace accented characters in python?

查看:65
本文介绍了如何替换python中的重音字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的输出看起来像àéêöhello!".我需要像这样更改我的输出 'aeeohello',只需将字符 à 替换为这样.

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

推荐答案

嗨 Ganesh 请使用下面的代码.

Hi Ganesh Please Use the below code.

它对我有用!

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

这篇关于如何替换python中的重音字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆