Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么isdigit()在浮点时返回false?_Python_Python 2.7_Floating Point_Digit - Fatal编程技术网

Python 为什么isdigit()在浮点时返回false?

Python 为什么isdigit()在浮点时返回false?,python,python-2.7,floating-point,digit,Python,Python 2.7,Floating Point,Digit,我想检查我的值是带点的浮点值还是带逗号的浮点值,但isdigit()返回带点的false。我想知道为什么以及如何度过这段时间 > value = "0.0" > print value.isdigit(): >>> False 我的代码是: if "." in value and value.isdigit() print "ok" str.isdigit()仅当字符串中的所有字符都是数字时才会返回true是标点符号,不是数字 从Python 3: 形式

我想检查我的值是带点的浮点值还是带逗号的浮点值,但isdigit()返回带点的false。我想知道为什么以及如何度过这段时间

> value = "0.0"
> print value.isdigit():
>>> False
我的代码是:

if "." in value and value.isdigit()
    print "ok"
str.isdigit()
仅当字符串中的所有字符都是数字时才会返回true<代码>是标点符号,不是数字

从Python 3:

形式上,数字是一个属性值Numeric\u Type=digit或Numeric\u Type=Decimal的字符

(对于Python 2,对于
str
对象,只考虑ASCII数字(
0
9
),但是对于
unicode
对象,相同的定义适用)

看,;有一些与描述相符

将其简化为一般unicode类别,unicode中的数字类型有一个以
N
开头的类别,但
没有:

>>> import unicodedata
>>> unicodedata.category(u'.')
'Po'
p
在这里代表标点符号,
o
代表其他符号

反之亦然,仅包含数字的字符串并不总是可转换为浮点数或数字:

>>> unicodedata.name(u'\u2080')
'SUBSCRIPT ZERO'
>>> unicodedata.category(u'\u2080')
'No'
>>> unicodedata.digit(u'\u2080')
0
>>> u'\u2080'.isdigit()
True
>>> float(u'\u2080')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'decimal' codec can't encode character u'\u2080' in position 0: invalid decimal Unicode string

因为
不是数字
isdigit()
仅对仅包含数字的字符串返回true。这在任何时候都不可能是真的,因为一个东西不能与另一个结合在一起。但是“0.0”是一个浮点数,所以对我来说它也是一个数字还是不同?
“0.0”
不是一个数字<代码>“0”是一个数字<代码>“0.0”是一个包含两位数字和一个
字符的字符串。请特别参阅定义3。“数字”并不意味着“数字”是的,始终使用异常名称捕获except块中的各个异常+1.
def is_float(string):
    try:
        float(string)
        return True
    except ValueError:
        return False