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

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. c++基础: uint8_t uint16_t uint32_t uint64_t size_t ssize_t数据类型

    https://blog.csdn.net/lzx_bupt/article/details/7066577 在nesc的代码中,你会看到很多你不认识的数据类型,比如uint8_t等.咋一看,好像是个 ...

  2. Vulnhub 靶场 BEELZEBUB: 1

    Vulnhub 靶场 BEELZEBUB: 1 前期准备 靶机地址:https://www.vulnhub.com/entry/beelzebub-1,742/ kali攻击机ip:192.168.1 ...

  3. MFC程序运行原理初探

    几年前,写过一段时间的MFC,但是只知其然不知其所以然,最近闲来无事,研究了一下MFC程序的运行顺序,特此记录一下. 首先,如果我们创建一个MFC程序的话,首先会自动生成一个CWinApp的子类,程序 ...

  4. import cv2时出现ImportError: DLL load fail:找不到指定模块

  5. sap IUT240 Contract Accounts Receivable and Payable pdf

    sap IUT240 Contract Accounts Receivable and Payable  pdf sap IUT240 Contract Accounts Receivable and ...

  6. chrome网页截图

    1. 打开网页 2. F12,打开开发者工具 3. Ctrl+Shift+P,打开应用查找框 4. 输入screenshot,查找相关工具 5. 试用工具 1)Capture area screens ...

  7. 7. C语言科学计数法表示int

    c语言10的n次方写用e表示: 比如int a=10e2 表示10*10的2次方=1000: 注意:10e6, 代表10*10^6 不代表10^6. 10^6为1^6

  8. 手写简单call、apply、bind

    1.call ~function(){ function call_1(context, ...args){ context = context == undefined ? window : con ...

  9. windows 批量杀进程

    1 import psutil 2 from subprocess import Popen, PIPE 3 4 process_name ="bsmr.exe,fxclient.exe,F ...

  10. calibredrv命令

    flattencell: set L1 [layout create *.gds -dt_expand] $L1 flatten cell TOP_CELL_NAME $L1 gdsout ./*_f ...