Python, Determine if a string should be converted into Int or Float
def isfloat(x):
try:
a = float(x)
except (TypeError, ValueError):
return False
else:
return True
def isint(x):
try:
a = float(x)
b = int(a)
except (TypeError, ValueError):
return False
else:
return a == b
Python float
objects have an is_integer
method:
from ast import literal_eval
def parses_to_integer(s):
val = literal_eval(s)
return isinstance(val, int) or (isinstance(val, float) and val.is_integer())