python通过自定义isnumber函数判断字符串是否为数字的方法

yipeiwu_com6年前Python基础

本文实例讲述了python通过自定义isnumber函数判断字符串是否为数字的方法。分享给大家供大家参考。具体如下:

''' isnumeric.py
test a numeric string s if it's usable
for int(s) or float(s)
'''
def isnumeric(s):
  '''returns True if string s is numeric'''
  return all(c in "0123456789.+-" for c in s)
# test module ...
if __name__ == '__main__':
  print(isnumeric('123'))   # True
  print(isnumeric('-123.45')) # True
  print(isnumeric('+3.14'))  # True
  print(isnumeric('$99.95'))  # False

希望本文所述对大家的Python程序设计有所帮助。

相关文章

Tensorflow Summary用法学习笔记

最近在研究tensorflow自带的例程speech_command,顺便学习tensorflow的一些基本用法。 其中tensorboard 作为一款可视化神器,可以说是学习tenso...

Python 文件操作之读取文件(read),文件指针与写入文件(write),文件打开方式示例

Python 文件操作之读取文件(read),文件指针与写入文件(write),文件打开方式示例

本文实例讲述了Python 文件操作之读取文件(read),文件指针与写入文件(write),文件打开方式。分享给大家供大家参考,具体如下: demo.py(读取文件): # 1....

python 搜索大文件的实例代码

如下所示: import os,os.path def getBigFile(pathname,filesize):#第一个参数为要遍历的文件夹,第二个是要找的最小文件的大小...

python识别图像并提取文字的实现方法

python识别图像并提取文字的实现方法

前言 python图像识别一般基础到的就是tesseract了,在爬虫中处理验证码广泛使用。 安装 安装教程网上大都差不多,Windows下确实比较麻烦,涉及到各种路径、环境变量甚至与l...

python+opencv实现霍夫变换检测直线

python+opencv实现霍夫变换检测直线

本文实例为大家分享了python+opencv实现霍夫变换检测直线的具体代码,供大家参考,具体内容如下 python+opencv实现高斯平滑滤波 python+opencv实现阈值分...