python isdigit() 函数为非数字字符 u'\u2466' 返回 true

标签 python unicode digit

我在处理 python isdigit 函数时遇到了一个奇怪的问题。

例如:

>>> a = u'\u2466'
>>> a.isdigit()
Out[1]: True
>>> a.isnumeric()
Out[2]: True

为什么这个字符是数字?

有什么方法可以让它返回 False,谢谢?


编辑,如果我不想把它当作一个数字,那么如何过滤掉呢?

例如,当我尝试将其转换为 int 时:

>>> int(u'\u2466')

然后 UnicodeEncodeError 发生了。

最佳答案

U+2466 是 CIRCLED DIGIT SEVEN (⑦),所以是的,它是一个数字。

如果您对数字的定义与 Unicode Consortium 的定义不同,您可能必须编写自己的 isdigit() 方法。

Edit, If I don't want to treat it as a digit, then how to filter it out?

如果您只对 ASCII 数字 0...9 感兴趣,您可以这样做:

In [4]: s = u'abc 12434 \u2466 5 def'

In [5]: u''.join(c for c in s if '0' <= c <= '9')
Out[5]: u'124345'

关于python isdigit() 函数为非数字字符 u'\u2466' 返回 true,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26285447/

相关文章:

python - 如何在pygame中处理移动对象而不必不断重绘背景

python - 从 Python pandas 中的空间数据创建 bin - 可能使用 groupby、diff 和 cut?

python - PyQt QListWidget 自定义项

php - 获取 iconv 来转换我的字符串

c++ - 如何使用 ov_open_callbacks 从流中打开 Ogg Vorbis 文件

opencv - 如何分类是单个数字还是多个数字

C:如何将多位数字分解成单独的变量?

python - 按索引选择不同形状的numpy数组并将其写回

Python 使用 euc-kr 编码以意想不到的方式对(韩语)字符进行编码(编解码器、编码模块)

c++ double /精度