我有一个表单值:
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
如何检查数值?
答案 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