Created
January 13, 2015 01:16
-
-
Save rkennesson/36c78975891619d8740f to your computer and use it in GitHub Desktop.
check if input is a number
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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