为什么 `'↊'.isnumeric()` 为 false?

2024-01-09

根据官方 Unicode 联盟代码表 http://www.unicode.org/charts/PDF/U2150.pdf,所有这些都是数字:

⅐   ⅑   ⅒   ⅓   ⅔   ⅕   ⅖   ⅗   ⅘   ⅙   ⅚   ⅛   ⅜   ⅝   ⅞   ⅟
Ⅰ   Ⅱ   Ⅲ   Ⅳ   Ⅴ   Ⅵ   Ⅶ   Ⅷ   Ⅸ   Ⅹ   Ⅺ   Ⅻ   Ⅼ   Ⅽ   Ⅾ   Ⅿ
ⅰ   ⅱ   ⅲ   ⅳ   ⅴ   ⅵ   ⅶ   ⅷ   ⅸ   ⅹ   ⅺ   ⅻ   ⅼ   ⅽ   ⅾ   ⅿ
ↀ   ↁ   ↂ   Ↄ   ↄ   ↅ   ↆ   ↇ   ↈ   ↉   ↊   ↋

然而,当我要求 Python 告诉我哪些是数字时,它们都是数字(甚至)除了四个:

In [252]: print([k for k in "⅐⅑⅒⅓⅔⅕⅖⅗⅘⅙⅚⅛⅜⅝⅞⅟ⅠⅡⅢⅣⅤⅥⅦⅧⅨⅩⅪⅫⅬⅭⅮⅯⅰⅱⅲⅳⅴⅵⅶⅷⅸⅹⅺⅻⅼⅽⅾⅿↀↁↂↃↄↅↆↇↈ↉↊↋" if not k.isnumeric()])
['Ↄ', 'ↄ', '↊', '↋']

那些是:

  • Ↄ 罗马数字倒转一百
  • ↄ 拉丁文小写字母反转 C
  • ↊ 变成数字二
  • ↋ 变成数字三

为什么 Python 认为这些不是数字?


str.isnumeric https://docs.python.org/3/library/stdtypes.html?highlight=isnumeric#str.isnumeric对于“具有 Unicode 数值属性的所有字符”,记录为 true。

The canonical reference for that property is the Unicode Character Database http://www.unicode.org/ucd/. The information we need can be dug out of http://www.unicode.org/Public/9.0.0/ucd/UnicodeData.txt http://www.unicode.org/Public/9.0.0/ucd/UnicodeData.txt , which is the latest version at time of writing (late 2016) (warning: 1.5MB text file). It's a little tricky to read (the documentation is in UAX#44 http://www.unicode.org/reports/tr44/). I'm going to show its entry for a character that is numeric first, U+3023 HANGZHOU NUMERAL THREE ()

3023;HANGZHOU NUMERAL THREE;Nl;0;L;;;;3;N;;;;;

第八个分号分隔字段是“数值”属性;在本例中,它的值为3,与角色的名字一致。蟒蛇的str.isnumeric当且仅当该字段非空时才为 true。可以直接使用查询unicodedata.numeric https://docs.python.org/3/library/unicodedata.html#unicodedata.numeric.

The third以分号分隔的字段是一个两个字符的代码,给出“一般类别 http://www.unicode.org/reports/tr44/#General_Category_Values“;在本例中为“Nl”。大多数(但不是全部)具有数值的字符都属于“数字”类别之一(类别代码的第一个字母是 N)。例外情况是所有汉字,根据上下文,可能表示也可能不表示数字;请参阅UAX#38 http://www.unicode.org/reports/tr38/.

现在,您所询问的角色:

2183;ROMAN NUMERAL REVERSED ONE HUNDRED;Lu;0;L ;;;;;N;;;    ;2184;
2184;LATIN SMALL LETTER REVERSED C     ;Ll;0;L ;;;;;N;;;2183;    ;2183
218A;TURNED DIGIT TWO                  ;So;0;ON;;;;;N;;;    ;    ;
218B;TURNED DIGIT THREE                ;So;0;ON;;;;;N;;;    ;    ;

这些字符做not分配了一个数值,因此 Python 的行为是正确的。

注:每https://docs.python.org/3.6/whatsnew/3.6.html https://docs.python.org/3.6/whatsnew/3.6.html,Python只会在3.6版本中更新到Unicode 9.0.0;然而,AFAICT这些字符已经很长时间没有改变了。

(“为什么这些字符没有数值?”这个问题只有 Unicode 联盟才能明确回答;如果您有兴趣,我建议您在他们的一个网站上提出这个问题)邮件列表 http://www.unicode.org/consortium/distlist.html.)

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

为什么 `'↊'.isnumeric()` 为 false? 的相关文章

随机推荐