这个函数是我在网上找到,时间太长了忘了那个文章了。用着很稳定,所以做个笔记记录一下,以便以后自己再用直接抄一下就行。
def is_number(s):
try:
float(s)
return True
except ValueError:
pass
try:
import unicodedata
unicodedata.numeric(s)
return True
except (TypeError, ValueError):
pass
return False
主函数直接调用即刻
def main():
str = '1'
if is_number(str):
print('这是数字')
else:
print('这不是数字')