python unidecode - 如何使用

时间:2017-09-01 20:18:01

标签: python non-ascii-characters

使用python,给定string =“Tiësto& Sevenn - BOOM(Artelax Remix)”,其中包含非ascii字符,如何使用unidecode修复字符串,以便删除非ascii字符?

{{1}}

上面的剪辑给了我: artistname =Tiësto& Sevenn songname = BOOM(Artelax Remix)

如您所见,artistname仍包含非ascii字符。如何使用unidecode解决此问题?

1 个答案:

答案 0 :(得分:2)

只需在字符串上调用unidecode不加引号):

>>> from unidecode import unidecode
>>> unidecode(string)
'Tiesto & Sevenn - BOOM (Artelax Remix)'

在归一化为分解形式后,还有更长/更慢的删除组合字符的路径:

>>> import unicodedata
>>> ''.join(s for s in unicodedata.normalize('NFD', string) if not unicodedata.combining(s))
'Tiesto & Sevenn - BOOM (Artelax Remix)'