判断字符串是否是数值的传统代码参考:

sInput=input("请输入数值[整数或小数]:")
sInput2=""
bErr=False
cSign=""
if(sInput.startswith("+")):
sPlus=sInput.count("+")
if(sPlus==1):
sInput2=sInput.lstrip("+")
cSign="+"
else:
bErr=True
elif(sInput.startswith("-")):
sSub=sInput.count("-")
if(sSub==1):
sInput2=sInput.lstrip("-")
cSign="-"
else:
bErr=True
else:
sChar=sInput[0:1]
if(sChar.isdigit()==False):
bErr=True
else:
sInput2=sInput if(bErr):
print("数值格式不正确.")
elif(sInput2.isdigit()):
print("数值格式正确.")
else:
pointNum=sInput2.count(".")
if(pointNum==1):
s2=sInput2.replace(".","")
if(s2.isdigit()):
print("数值格式正确.")
else:
print("数值格式不正确.")
else:
print("数值格式不正确.")
if(bErr==False):
print(cSign+sInput2)

采用正则表达式来判断字符串是否是数值的参考代码:

import re

strInput = '310.1'
strList = re.findall('^[\-\+]?\d+\.?\d+$',strInput)
print(strList)
strInput2 = 'aa310.0.1'
strList2 = re.search('[\-\+]?\d+\.?\d+',strInput2)
print(strList2.group()) strInput3 = '310.0.1'
strList3 = re.match('[\-\+]?\d+\.?\d+',strInput3)
print(strList3.group()) strInput4 = '310.13'
strList4 = re.match('^[\-\+]?\d+\.?\d+$',strInput4) if(strList4):
res=strList4.group()
print(res)
else:
print("Err")strInput5 = '310.01a'
strInput5 = '310.13'
strList5 = re.fullmatch('[\-\+]?\d+\.?\d+',strInput5)
if(strList5):
print(strList5.group())
else:
print("Err")

[Python]判断字符串是否是数值,可以采用正则表达式技术来进行输入检测处理的更多相关文章

  1. python判断字符串

    python判断字符串 s为字符串s.isalnum() 所有字符都是数字或者字母s.isalpha() 所有字符都是字母s.isdigit() 所有字符都是数字s.islower() 所有字符都是小 ...

  2. python判断字符串是否为空的方法s.strip()=='' if not s.strip():

    python 判断字符串是否为空用什么方法? 复制代码 s=' ' if s.strip()=='':     print 's is null' 或者 if not s.strip():     p ...

  3. python 判断字符串中是否只有中文字符

    python 判断字符串中是否只有中文字符 学习了:https://segmentfault.com/q/1010000007898150 def is_all_zh(s): for c in s: ...

  4. Python判断字符串编码以及编码的转换

    转自:http://www.cnblogs.com/zhanhg/p/4392089.html Python判断字符串编码以及编码的转换 判断字符串编码: 使用 chardet 可以很方便的实现字符串 ...

  5. python判断字符串中是否包含子字符串

    python判断字符串中是否包含子字符串 s = '1234问沃尔沃434' if s.find('沃尔沃') != -1:     print('存在') else:     print('不存在' ...

  6. python判断字符串是否是json格式方法分享

    python判断字符串是否是json格式方法分享 在实际工作中,有时候需要对判断字符串是否为合法的json格式 解决方法使用json.loads,这样更加符合'Pythonic'写法 代码示例:   ...

  7. Java判断字符串是否是数值

    判断一个字符串是否是数值,可以用正则表达式来判断.更简单的方法是把字符串转换成Float或者Double,然后捕捉NumberFormatException错误,如果有错误,就说明不是一个数值,如果没 ...

  8. Python判断字符串是否为字母或者数字

    严格解析:有除了数字或者字母外的符号(空格,分号,etc.)都会Falseisalnum()必须是数字和字母的混合isalpha()不区分大小写 str_1 = "123" str ...

  9. python判断字符串是否为空和null

    1.使用字符串长度判断 len(s==0)则字符串为空 test1 = '' if len(test1) == 0: print('test1为空串') else: print('test非空串,te ...

  10. python判断字符串,str函数isdigit、isdecimal、isnumeric的区别

    s为字符串s.isalnum() 所有字符都是数字或者字母s.isalpha() 所有字符都是字母s.isdigit() 所有字符都是数字s.islower() 所有字符都是小写s.isupper() ...

随机推荐

  1. 【基础】为何odd negative scaling会导致Unity动态合批失败?

    https://blog.csdn.net/weixin_41885426/article/details/109817466

  2. ComPiler200003:Story-Oriented Programming

    Story-Oriented Programming MAY 25TH, 2018 http://www.brandonkeown.com/2018/05/story-oriented-program ...

  3. python pip 下载出问题解决方法(超时出错)

    问题如下, 百度了一下找到了解决方法 这样的原因是连接不到国外的安装源,在下载的代码行中手动输入国内安装源 例如: pip install itchat -i https://pypi.tuna.ts ...

  4. 移动端性能测试--CPU资源

    一.背景 在很多场景下我们去使用 App,可能会碰到手机会出现发热发烫的现象.这是因为 CPU 使用率过高.CPU 过于繁忙,会使得整个系统无法响应用户,整体性能降低,用户体验变得相当差,也容易引起 ...

  5. VC 运行时库的引用区别 /MD、/MDd 和 /MT、/MTd和DllMain函数

    /MT和/MTd表示采用多线程CRT库的静态lib版本.该选项会在编译时将运行时库以静态lib的形式完全嵌入.该选项生成的可执行文件运行时不需要运行时库dll的参加,会获得轻微的性能提升,但最终生成的 ...

  6. window java 字节码修改

    前提:  win10 安装包: 1.  JDK10.0.1.msi (包含jre环境),    JDK的安装路径 2.  jclasslib 6.0.3    用于编辑常量 3.JBE    用于编辑 ...

  7. Linux - 查看、修改、更新系统时间(自动同步网络时间)

    系统:Centos7 1.查看系统时间 执行 date 命令可以查看当前系统的时间: 执行 hwclock 命令可以查看当前系统的时间 2.手动修改系统时间 (1)执行如下命令可以设置一个新的系统时间 ...

  8. 根据STM32CubeMX软件创建STM32芯片的Altium Designer原理图库教程-转载

    (24条消息) 根据STM32CubeMX软件创建STM32芯片的Altium Designer原理图库教程_奥比克_阿彦少爷的博客-CSDN博客_stm32原理图库 1所需软件及环境1.STM32C ...

  9. javaSE-验证码生成

    一.使用Math类的radom() 方法 //生成验证码 String verifcationCode = ""; for (int i = 0; i <= 5; i++) ...

  10. Vue路由跳转时定位到页面顶部

    代码如下: router.afterEach((to, from, next) => { document.body.scrollTop = 0; document.documentElemen ...