2015-04-04 60 views
0

在python字符串中有一个方法isnumericisdigitisnumeric&isdecimal的行为不如预期

我受到(错误的)印象,isnumericisdecimal将返回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 

而且我发现isnumericisdecimal返回真,如果字符串中的所有字符都是整数,但假如果'.'(点)存在。什么导致这种行为?不应该'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() 
+2

您是否检查['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

+1

另请参阅http://stackoverflow.com/questions/22789392/isdecimal-and-isdigit-difference-example – cdarke 2015-04-04 06:35:16

回答

2

Python documentationisnumeric返回True限定,如果该字符串内的所有字符是数字,否则False。点不被认为是数字。