Got an interesting situation where I had to distinguish if the piece of string is float or date in Python 2.7. No problems to do that when there is dateutil.parse library in Python. But in my case the tricky situation was that piece of string which actually is float, can be interpreted as... date.
So, I came to solution - let there be two separate functions.
Checking if the string is float is straightforward.
So, I came to solution - let there be two separate functions.
Checking if the string is float is straightforward.
def is_number(s):
try:
float(s)
return True
except ValueError:
return False
Checking if the string is date - first of all, I check if it is a float. And if it is - lets return False. def is_date(d):
if is_number(d):
return False
try:
parse(d)
print parse(d)
return True
except ValueError:
return False
And that`s it.