[Python]判断字符串是否是数值,可以采用正则表达式技术来进行输入检测处理
判断字符串是否是数值的传统代码参考:
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]判断字符串是否是数值,可以采用正则表达式技术来进行输入检测处理的更多相关文章
- python判断字符串
python判断字符串 s为字符串s.isalnum() 所有字符都是数字或者字母s.isalpha() 所有字符都是字母s.isdigit() 所有字符都是数字s.islower() 所有字符都是小 ...
- python判断字符串是否为空的方法s.strip()=='' if not s.strip():
python 判断字符串是否为空用什么方法? 复制代码 s=' ' if s.strip()=='': print 's is null' 或者 if not s.strip(): p ...
- python 判断字符串中是否只有中文字符
python 判断字符串中是否只有中文字符 学习了:https://segmentfault.com/q/1010000007898150 def is_all_zh(s): for c in s: ...
- Python判断字符串编码以及编码的转换
转自:http://www.cnblogs.com/zhanhg/p/4392089.html Python判断字符串编码以及编码的转换 判断字符串编码: 使用 chardet 可以很方便的实现字符串 ...
- python判断字符串中是否包含子字符串
python判断字符串中是否包含子字符串 s = '1234问沃尔沃434' if s.find('沃尔沃') != -1: print('存在') else: print('不存在' ...
- python判断字符串是否是json格式方法分享
python判断字符串是否是json格式方法分享 在实际工作中,有时候需要对判断字符串是否为合法的json格式 解决方法使用json.loads,这样更加符合'Pythonic'写法 代码示例: ...
- Java判断字符串是否是数值
判断一个字符串是否是数值,可以用正则表达式来判断.更简单的方法是把字符串转换成Float或者Double,然后捕捉NumberFormatException错误,如果有错误,就说明不是一个数值,如果没 ...
- Python判断字符串是否为字母或者数字
严格解析:有除了数字或者字母外的符号(空格,分号,etc.)都会Falseisalnum()必须是数字和字母的混合isalpha()不区分大小写 str_1 = "123" str ...
- python判断字符串是否为空和null
1.使用字符串长度判断 len(s==0)则字符串为空 test1 = '' if len(test1) == 0: print('test1为空串') else: print('test非空串,te ...
- python判断字符串,str函数isdigit、isdecimal、isnumeric的区别
s为字符串s.isalnum() 所有字符都是数字或者字母s.isalpha() 所有字符都是字母s.isdigit() 所有字符都是数字s.islower() 所有字符都是小写s.isupper() ...
随机推荐
- R7-7 调查电视节目受欢迎程度
R7-7 调查电视节目受欢迎程度 分数 15 全屏浏览题目 切换布局 作者 颜晖 单位 浙大城市学院 某电视台要调查观众对该台8个栏目(设相应栏目编号为1~8)的受欢迎情况,共调查了n位观众(1≤n≤ ...
- win7下virtualbox虚拟机中安装centos后设置共享文件夹
报错信息: building the main Guest Additions module FAILEDunable to find the sources of your current Linu ...
- cv::eigen
void TestEigen() { cv::Mat m = (cv::Mat_<float>(3, 3) << 1, 2, 3, 2, 5, 6, 3, 6, 7); cv: ...
- math公式手写识别网址
math公式手写识别网址 参考:https://webdemo.myscript.com/views/math/index.html
- android 集成友盟实现 第三方分享 登录(qq,新浪,微信)
其实友盟的文档写的非常详细了,在这只是记录一下开发过程中遇到过的坑. 开发流程,先到友盟的官网注册账号创建应用,友盟的文档地址:http://dev.umeng.com/social/android/ ...
- MVC对session或cookie保存的值在js中做处理
在cshtml中保存数据 eg: @Html.Hidden("sessionUserName", Session["userName"]) @Html.Hidd ...
- mqtt安装和使用
linux下 下载: wget https://www.emqx.io/downloads/broker/v3.2.1/emqx-centos7-v3.2.1.zip 解压:unzip emqx-ce ...
- Guava Retry重试机制
1.添加pom依赖 <dependency> <groupId>com.github.rholder</groupId> <artifactId>gua ...
- 8.golang语言学习,运算符介绍
1.算术运算 自增,自减,只能单独使用,++,--只能写在变量后面 2.赋值运算符 优先级,单目运算,赋值运算从右到左运算,其余从左到右,无三目运算,用if实现 3.比较运算符/关系 4.逻辑运算符 ...
- centos7编译安装LNMP服务架构
CentOS7.4 源码编译安装LNMP 1.基于CentOS7.4源码编译安装得lnmp 系统环境CentOS 7.4 系统最小化安装,只安装了一些常用包(vim.lirzs.gcc*.wget. ...