我在处理 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/