Skip to content

Instantly share code, notes, and snippets.

@rkennesson
Created January 13, 2015 01:16
Show Gist options
  • Save rkennesson/36c78975891619d8740f to your computer and use it in GitHub Desktop.
Save rkennesson/36c78975891619d8740f to your computer and use it in GitHub Desktop.
check if input is a number
#http://stackoverflow.com/questions/354038/how-do-i-check-if-a-string-is-a-number-in-python
def is_number(s):
try:
n=str(float(s))
if n == "nan" or n=="inf" or n=="-inf" : return False
except ValueError:
try:
complex(s) # for complex
except ValueError:
return False
return True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment