删除重音和特殊字符 [英] removing accent and special characters

查看:110
本文介绍了删除重音和特殊字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:



Python和字符规范化

我想删除重音符号,将所有字符都转换为小写,并删除任何数字和特殊字符。

I would like to remove accents, turn all characters to lowercase, and delete any numbers and special characters.

示例:

Frédér8ic@->弗雷德里克

Frédér8ic@ --> frederic

建议:

def remove_accents(data):
    return ''.join(x for x in unicodedata.normalize('NFKD', data) if \
    unicodedata.category(x)[0] == 'L').lower()

还有更好的方法吗?

推荐答案

可能的解决方案ld be

A possible solution would be

def remove_accents(data):
    return ''.join(x for x in unicodedata.normalize('NFKD', data) if x in string.printable).lower()

使用NFKD AFAIK是标准化unicode以将其转换为兼容字符的标准方法。剩下的就是删除源自规范化的特殊字符数字和unicode字符,您可以简单地与 string.ascii_letters 进行比较,并删除该集中没有的任何字符。

Using NFKD AFAIK is the standard way to normalize unicode to convert it to compatible characters. The rest as to remove the special characters numbers and unicode characters that originated from normalization, you can simply compare with string.ascii_letters and remove any character's not in that set.

这篇关于删除重音和特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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