为什么“ǃ”.isalpha()为真而“!”.isalpha()为假?

2024-05-29 05:15:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我刚刚在解析来自IANA的数据时发现了这种奇怪的行为

"ǃ".isalpha() # returns True
"!".isalpha() # returns False

显然,这两个感叹号是不同的:

In [62]: hex(ord("ǃ"))                                                          
Out[62]: '0x1c3'

In [63]: hex(ord("!"))                                                          
Out[63]: '0x21'

只是想知道有没有办法防止这种情况发生?这种行为的根源是什么


Tags: 数据infalsetrue情况outreturnsiana
2条回答

从文档:

^{}

Return True if all characters in the string are alphabetic and there is at least one character, False otherwise. Alphabetic characters are those characters defined in the Unicodecharacter database as “Letter”, i.e., those with general category property being one of “Lm”, “Lt”, “Lu”, “Ll”, or “Lo”. Note that this is different from the “Alphabetic” property defined in the Unicode Standard.

这意味着您使用的utf字符在utf数据库中定义为字母

>>> ord("ǃ")
   451

查看Wikipedia List of UTF characters,字符ǃ位于Latin Extended B之下,这就是为什么isalpha返回True

检查Unicode Database中的字符。类似于ǃ\u1c3)的感叹号是一个字母:

import unicodedata
for c in "!ǃ":
    print(c,'{:04x}'.format(ord(c)),unicodedata.category(c), unicodedata.name(c))
! 0021 Po EXCLAMATION MARK
ǃ 01c3 Lo LATIN LETTER RETROFLEX CLICK

相关问题 更多 >

    热门问题