[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() ...
随机推荐
- linux kali 报错Intel VT-x
报错原因:第一次安装虚拟机intel vt-x 处于禁用状态,没有启用 分析思路:找到 Intel vt - x 位置,并打开 .(intel - x在bios) 解决方法: 联想笔记本为例:进入bi ...
- SQLSERVER 根据一个库的视图在另一个库中生成一张表
select * into VPsiOuntStockBill from [KshDbPro].dbo.VPsiOuntStockBill
- java第六周学习情况
这个星期接到了要开学的消息,心情是非常慌张的,毕竟还没有学到多少东西.但确实是要开学了.心中说不出激动还是紧张,那就带着这种奇妙的情绪记录这次的学习吧 首先必然还是看了相关的程序,记没记下来是另外一说 ...
- Go 的位操作
在计算机内存昂贵,处理能力有限的美好旧时光里,用比较黑客范的位运算方式去处理信息是首选方式(某些情况下只能如此).时至今日,直接使用位运算仍然是很多计算领域中不可或缺的部分,例如底层系统编程,图形处理 ...
- SignalR《二》接着前篇的继续
SignalR<二>接着前篇的继续 SignalR身份验证 在ChatRoomHub加上[Authorize] 这样登录了才能发送消息 using Microsoft.AspNetCor ...
- Assetbundle 打包加载场景
public class CreateAssetBundle : Editor{ // 打包unity场景文件 [MenuItem("Assets/Build AssetBundle Sce ...
- iOS开发之定时器和tableview滑动阻塞问题
NSTimer *timer = [[NSTimer alloc]initWithFireDate:[NSDate distantPast] interval:1 target:self select ...
- postman导出Collection文件
postman接口调用工具可以将曾经使用过的请求配置导出为文件保存,方法如下: 1.编写一个接口测试用例 2.按组分类 3.导出 参考来源: https://blog.csdn.net/IBLiplu ...
- react module.scss文件中弹窗中 keyframes动画不生效,
以下修改,亲测有效非弹窗内动画写法 .submit_btn{ animation: submit_btn 1.5s infinite; -webkit-animation: submit_ ...
- Coursera Programming Languages, Part B 华盛顿大学 Week 3
ML Versus Racket 同 函数编程模式 (with constructs that encourage a functional style) 不鼓励 mutation (但提供了支持 m ...