python检查数值

时间:2015-06-10 03:02:34

标签: python django string

我有一个表单值:

anytext = request.POST.get("my_text", None)
if anytext and anytext.is_string:
    perform any action
if anytext and anytext.is_numeric:
    perform another action

如何检查数值?

2 个答案:

答案 0 :(得分:2)

您可以使用isdigit(),假设anytext始终为string类型。

例如:

'Hello World'.isdigit()  # returns False
'1234243'.isdigit()  # returns True

所以使用你的代码:

anytext = request.POST.get("my_text", "")
if anytext.isdigit():
    # perform action on numeric string
else:
    # perform action on alphanumeric string

答案 1 :(得分:0)

您可以尝试:

 if int(re.search(r'\d+', anytext).group())):
    #perform action
相关问题