0
在python字符串中有一个方法isnumeric
和isdigit
。isnumeric&isdecimal的行为不如预期
我受到(错误的)印象,isnumeric
和isdecimal
将返回True
的字符串,如123.45
,否则返回false。我跑蟒蛇以下版本:
Python 3.4.2 (default, Jan 7 2015, 11:54:58)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.56)] on darwin
而且我发现isnumeric
和isdecimal
返回真,如果字符串中的所有字符都是整数,但假如果'.'
(点)存在。什么导致这种行为?不应该'123.45'.isnumeric()
返回True
?
>>> mystr_a = '123.45'
>>> mystr_b = '123'
>>>
>>> mystr_a.isnumeric()
False
>>> mystr_a.isdecimal()
False
>>> mystr_b.isnumeric()
True
>>> mystr_b.isdecimal()
您是否检查['isnumeric'](https://docs.python.org/3/library/stdtypes.html#str.isnumeric)和['isdecimal'](https://docs.python.org /3/library/stdtypes.html#str.isdecimal)文档?这是记录的行为。 – thefourtheye 2015-04-04 06:25:19
另请参阅http://stackoverflow.com/questions/22789392/isdecimal-and-isdigit-difference-example – cdarke 2015-04-04 06:35:16