是否有规则有助于查找与ascii相关的所有重音字母的UTF-8代码?例如,我可以使用字母é
的UTF-8代码中的所有重音字母è
,e
,...来获取所有UTF-8代码吗?
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),
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'
答案 0 :(得分:1)
它们通常被认为是许多语言中的不同角色。 但是,如果您真的需要这个,您将需要找到一个规范化字符串的函数。 在这种情况下,您需要规范化以获取分解的字符,其中这些字符在字符串中成为两个Unicode代码点。
答案 1 :(得分:0)
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"))
要反过来,可以使用unicodedata.normalize和NFD表格并取第一个字符,因为第二个字符是组合形式的重音。
print(unicodedata.normalize("NFD","è")[0]) # Prints "e".