最近在学点统计学,读入数据的时候有文本,有数字,如果是数字字符串则需要进行转换,然而发现python中的string的isdigit()方法只能判断整数,对于小数是没有办法处理的,没办法,只能自己动手了:
import re
def isnumber(num):
regex = re.compile(r"^(-?\d+)(\.\d*)?$")
if re.match(regex,n):
return True
else:
return False
好了,搞定收工。
最近在学点统计学,读入数据的时候有文本,有数字,如果是数字字符串则需要进行转换,然而发现python中的string的isdigit()方法只能判断整数,对于小数是没有办法处理的,没办法,只能自己动手了:
import re
def isnumber(num):
regex = re.compile(r"^(-?\d+)(\.\d*)?$")
if re.match(regex,n):
return True
else:
return False
好了,搞定收工。