是否有一条规则可以帮助找到与 ascii 相关的所有重音字母的 UTF-8 代码?例如,我可以从字母的 UTF-8 代码中获得所有重音字母é
, è
,... 的所有 UTF-8 代码e
吗?
这是使用 Ramchandra Apte 上面给出的解决方案在 Python 3 中的展示
import unicodedata
def accented_letters(letter):
accented_chars = []
for accent_type in "acute", "double acute", "grave", "double grave":
try:
accented_chars.append(
unicodedata.lookup(
"Latin small letter {letter} with {accent_type}" \
.format(**vars())
)
)
except KeyError:
pass
return accented_chars
print(accented_letters("e"))
for kind in ["NFC", "NFKC", "NFD", "NFKD"]:
print(
'---',
kind,
list(unicodedata.normalize(kind,"é")),
sep = "\n"
)
for oneChar in "βεέ.¡¿?ê":
print(
'---',
oneChar,
unicodedata.name(oneChar),
在 Unicode 中查找字形相似的字符?
unicodedata.normalize('NFD', oneChar).encode('ascii','ignore'),
sep = "\n"
)
对应的输出。
['é', 'è', 'ȅ']
---
NFC
['é']
---
NFKC
['é']
---
NFD
['e', '́']
---
NFKD
['e', '́']
---
β
GREEK SMALL LETTER BETA
b''
---
ε
GREEK SMALL LETTER EPSILON
b''
---
έ
GREEK SMALL LETTER EPSILON WITH TONOS
b''
---
.
FULL STOP
b'.'
---
¡
INVERTED EXCLAMATION MARK
b''
---
¿
INVERTED QUESTION MARK
b''
---
?
QUESTION MARK
b'?'
---
ê
LATIN SMALL LETTER E WITH CIRCUMFLEX
b'e'