在Python中是否有一种标准方法来规范化unicode字符串,以便它只能理解可用于表示它的最简单的unicode实体?
我的意思是,一些东西,想翻译的顺序['LATIN SMALL LETTER A', 'COMBINING ACUTE ACCENT']来['LATIN SMALL LETTER A WITH ACUTE']?
看看问题出在哪里:
>>> import unicodedata
>>> char = "á"
>>> len(char)
1
>>> [ unicodedata.name(c) for c in char ]
['LATIN SMALL LETTER A WITH ACUTE']
Run Code Online (Sandbox Code Playgroud)
但现在:
>>> char = "a?"
>>> len(char)
2
>>> [ unicodedata.name(c) for c in char ]
['LATIN SMALL LETTER A', 'COMBINING ACUTE ACCENT']
Run Code Online (Sandbox Code Playgroud)
当然,我可以迭代所有字符并进行手动替换等,但效率不高,我很确定我会错过一半的特殊情况,并且会犯错误.
使用Python 2.5,我有一些文本存储在unicode对象中:
Dinis e Isabel,umadifı'cilrelacóa~oconjugalepolı'tica
这似乎是分解Unicode.Python中是否有通用的方法来反转分解,所以我最终得到:
Dinis e Isabel,umadifícilrelaçãoconclarationepolítica