python - python 3 的可靠 isnumeric() 函数是什么?

标签 python regex validation python-3.x isnumeric

我正在尝试做一些应该非常简单的事情,并检查 Entry 字段中的值是否是有效且真实的数字。 str.isnumeric() 方法不考虑“-”负数或“.”。十进制数。

我尝试为此编写一个函数:

def IsNumeric(self, event):
    w = event.widget
    if (not w.get().isnumeric()):
        if ("-" not in w.get()):
            if ("." not in w.get()):
                w.delete(0, END)
                w.insert(0, '')

在您返回并在其中键入字母之前,这一切都很好。然后就失败了。

我研究了使用 .split() 方法的可能性,但我找不到可靠的正则表达式来处理它。

这是一件非常正常的事情,需要完成。有什么想法吗?

最佳答案

try:
    float(w.get())
except ValueError:
    # wasn't numeric

关于python - python 3 的可靠 isnumeric() 函数是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30855314/

相关文章:

Python 3 - Zip 是 pandas 数据框中的迭代器

python - 如何在 OrderedDict 中迭代 OrderedDict

java - 正则表达式捕获组未按预期工作

ruby-on-rails - rails : calling standard validations from a custom validator

javascript - Bootstrap 4 Modal with Tabs 内的 JQuery Validation Form 不会提交

c# - 列表验证(数据注释)在客户端不起作用

python - E : Could not get lock/var/cache/apt/archives/lock - open (11: Resource temporarily unavailable)

python - 语法错误 ncurses + python3

javascript - 正则表达式提取 Javascript 中两个字符串之间的行

javascript - 如何使用正则表达式验证域名?