A regex or other string parsing method would be uglier and slower.
I’m not sure that anything much could be faster than the above. It calls the function and returns. Try/Catch doesn’t introduce much overhead because the most common exception is caught without an extensive search of stack frames.
The issue is that any numeric conversion function has two kinds of results
A number, if the number is valid
A status code (e.g., via errno) or exception to show that no valid number could be parsed.
C (as an example) hacks around this a number of ways. Python lays it out clearly and explicitly.
def is_number_tryexcept(s):""" Returns True is string is a number. """try:
float(s)returnTrueexceptValueError:returnFalseimport re
def is_number_regex(s):""" Returns True is string is a number. """if re.match("^\d+?\.\d+?$", s)isNone:return s.isdigit()returnTruedef is_number_repl_isdigit(s):""" Returns True is string is a number. """return s.replace('.','',1).isdigit()
funcs =[
is_number_tryexcept,
is_number_regex,
is_number_repl_isdigit
]
a_float ='.1234'print('Float notation ".1234" is not supported by:')for f in funcs:ifnot f(a_float):print('\t -', f.__name__)
浮点符号“ .1234”不受以下支持:
-is_number_regex
scientific1 ='1.000000e+50'
scientific2 ='1e50'print('Scientific notation "1.000000e+50" is not supported by:')for f in funcs:ifnot f(scientific1):print('\t -', f.__name__)print('Scientific notation "1e50" is not supported by:')for f in funcs:ifnot f(scientific2):print('\t -', f.__name__)
import timeit
test_cases =['1.12345','1.12.345','abc12345','12345']
times_n ={f.__name__:[]for f in funcs}for t in test_cases:for f in funcs:
f = f.__name__
times_n[f].append(min(timeit.Timer('%s(t)'%f,'from __main__ import %s, t'%f).repeat(repeat=3, number=1000000)))
测试以下功能的地方
from re import match as re_match
from re import compile as re_compile
def is_number_tryexcept(s):""" Returns True is string is a number. """try:
float(s)returnTrueexceptValueError:returnFalsedef is_number_regex(s):""" Returns True is string is a number. """if re_match("^\d+?\.\d+?$", s)isNone:return s.isdigit()returnTrue
comp = re_compile("^\d+?\.\d+?$")def compiled_regex(s):""" Returns True is string is a number. """if comp.match(s)isNone:return s.isdigit()returnTruedef is_number_repl_isdigit(s):""" Returns True is string is a number. """return s.replace('.','',1).isdigit()
TL;DR The best solution is s.replace('.','',1).isdigit()
I did some benchmarks comparing the different approaches
def is_number_tryexcept(s):
""" Returns True is string is a number. """
try:
float(s)
return True
except ValueError:
return False
import re
def is_number_regex(s):
""" Returns True is string is a number. """
if re.match("^\d+?\.\d+?$", s) is None:
return s.isdigit()
return True
def is_number_repl_isdigit(s):
""" Returns True is string is a number. """
return s.replace('.','',1).isdigit()
If the string is not a number, the except-block is quite slow. But more importantly, the try-except method is the only approach that handles scientific notations correctly.
funcs = [
is_number_tryexcept,
is_number_regex,
is_number_repl_isdigit
]
a_float = '.1234'
print('Float notation ".1234" is not supported by:')
for f in funcs:
if not f(a_float):
print('\t -', f.__name__)
Float notation “.1234” is not supported by:
– is_number_regex
scientific1 = '1.000000e+50'
scientific2 = '1e50'
print('Scientific notation "1.000000e+50" is not supported by:')
for f in funcs:
if not f(scientific1):
print('\t -', f.__name__)
print('Scientific notation "1e50" is not supported by:')
for f in funcs:
if not f(scientific2):
print('\t -', f.__name__)
Scientific notation “1.000000e+50” is not supported by:
– is_number_regex
– is_number_repl_isdigit
Scientific notation “1e50” is not supported by:
– is_number_regex
– is_number_repl_isdigit
EDIT: The benchmark results
import timeit
test_cases = ['1.12345', '1.12.345', 'abc12345', '12345']
times_n = {f.__name__:[] for f in funcs}
for t in test_cases:
for f in funcs:
f = f.__name__
times_n[f].append(min(timeit.Timer('%s(t)' %f,
'from __main__ import %s, t' %f)
.repeat(repeat=3, number=1000000)))
where the following functions were tested
from re import match as re_match
from re import compile as re_compile
def is_number_tryexcept(s):
""" Returns True is string is a number. """
try:
float(s)
return True
except ValueError:
return False
def is_number_regex(s):
""" Returns True is string is a number. """
if re_match("^\d+?\.\d+?$", s) is None:
return s.isdigit()
return True
comp = re_compile("^\d+?\.\d+?$")
def compiled_regex(s):
""" Returns True is string is a number. """
if comp.match(s) is None:
return s.isdigit()
return True
def is_number_repl_isdigit(s):
""" Returns True is string is a number. """
return s.replace('.','',1).isdigit()
There is one exception that you may want to take into account: the string ‘NaN’
If you want is_number to return FALSE for ‘NaN’ this code will not work as Python converts it to its representation of a number that is not a number (talk about identity issues):
>>> float('NaN')
nan
Otherwise, I should actually thank you for the piece of code I now use extensively. :)
which will return true only if there is one or no ‘.’ in the string of digits.
'3.14.5'.replace('.','',1).isdigit()
will return false
edit: just saw another comment …
adding a .replace(badstuff,'',maxnum_badstuff) for other cases can be done. if you are passing salt and not arbitrary condiments (ref:xkcd#974) this will do fine :P
It may take some getting used to, but this is the pythonic way of doing it. As has been already pointed out, the alternatives are worse. But there is one other advantage of doing things this way: polymorphism.
The central idea behind duck typing is that “if it walks and talks like a duck, then it’s a duck.” What if you decide that you need to subclass string so that you can change how you determine if something can be converted into a float? Or what if you decide to test some other object entirely? You can do these things without having to change the above code.
Other languages solve these problems by using interfaces. I’ll save the analysis of which solution is better for another thread. The point, though, is that python is decidedly on the duck typing side of the equation, and you’re probably going to have to get used to syntax like this if you plan on doing much programming in Python (but that doesn’t mean you have to like it of course).
One other thing you might want to take into consideration: Python is pretty fast in throwing and catching exceptions compared to a lot of other languages (30x faster than .Net for instance). Heck, the language itself even throws exceptions to communicate non-exceptional, normal program conditions (every time you use a for loop). Thus, I wouldn’t worry too much about the performance aspects of this code until you notice a significant problem.
回答 6
在Alfe指出您不需要单独检查float之后进行了更新,因为这两种情况都比较复杂:
def is_number(s):try:
complex(s)# for int, long, float and complexexceptValueError:returnFalsereturnTrue
先前曾说过:在极少数情况下,您可能还需要检查复数(例如1 + 2i),而复数不能用浮点数表示:
def is_number(s):try:
float(s)# for int, long and floatexceptValueError:try:
complex(s)# for complexexceptValueError:returnFalsereturnTrue
float.parse('giggity')// throws TypeException
float.parse('54.3')// returns the scalar value 54.3
float.tryParse('twank')// returns None
float.tryParse('32.2')// returns the scalar value 32.2
Note: You don’t want to return the boolean ‘False’ because that’s still a value type. None is better because it indicates failure. Of course, if you want something different you can change the fail parameter to whatever you want.
To extend float to include the ‘parse()’ and ‘try_parse()’ you’ll need to monkeypatch the ‘float’ class to add these methods.
If you want respect pre-existing functions the code should be something like:
For strings of non-numbers, try: except: is actually slower than regular expressions. For strings of valid numbers, regex is slower. So, the appropriate method depends on your input.
If you find that you are in a performance bind, you can use a new third-party module called fastnumbers that provides a function called isfloat. Full disclosure, I am the author. I have included its results in the timings below.
from __future__ import print_function
import timeit
prep_base = '''\
x = 'invalid'
y = '5402'
z = '4.754e3'
'''
prep_try_method = '''\
def is_number_try(val):
try:
float(val)
return True
except ValueError:
return False
'''
prep_re_method = '''\
import re
float_match = re.compile(r'[-+]?\d*\.?\d+(?:[eE][-+]?\d+)?$').match
def is_number_re(val):
return bool(float_match(val))
'''
fn_method = '''\
from fastnumbers import isfloat
'''
print('Try with non-number strings', timeit.timeit('is_number_try(x)',
prep_base + prep_try_method), 'seconds')
print('Try with integer strings', timeit.timeit('is_number_try(y)',
prep_base + prep_try_method), 'seconds')
print('Try with float strings', timeit.timeit('is_number_try(z)',
prep_base + prep_try_method), 'seconds')
print()
print('Regex with non-number strings', timeit.timeit('is_number_re(x)',
prep_base + prep_re_method), 'seconds')
print('Regex with integer strings', timeit.timeit('is_number_re(y)',
prep_base + prep_re_method), 'seconds')
print('Regex with float strings', timeit.timeit('is_number_re(z)',
prep_base + prep_re_method), 'seconds')
print()
print('fastnumbers with non-number strings', timeit.timeit('isfloat(x)',
prep_base + 'from fastnumbers import isfloat'), 'seconds')
print('fastnumbers with integer strings', timeit.timeit('isfloat(y)',
prep_base + 'from fastnumbers import isfloat'), 'seconds')
print('fastnumbers with float strings', timeit.timeit('isfloat(z)',
prep_base + 'from fastnumbers import isfloat'), 'seconds')
print()
Try with non-number strings 2.39108395576 seconds
Try with integer strings 0.375686168671 seconds
Try with float strings 0.369210958481 seconds
Regex with non-number strings 0.748660802841 seconds
Regex with integer strings 1.02021503448 seconds
Regex with float strings 1.08564686775 seconds
fastnumbers with non-number strings 0.174362897873 seconds
fastnumbers with integer strings 0.179651021957 seconds
fastnumbers with float strings 0.20222902298 seconds
As you can see
try: except: was fast for numeric input but very slow for an invalid input
I know this is particularly old but I would add an answer I believe covers the information missing from the highest voted answer that could be very valuable to any who find this:
For each of the following methods connect them with a count if you need any input to be accepted. (Assuming we are using vocal definitions of integers rather than 0-255, etc.)
x.isdigit()
works well for checking if x is an integer.
x.replace('-','').isdigit()
works well for checking if x is a negative.(Check – in first position)
x.replace('.','').isdigit()
works well for checking if x is a decimal.
x.replace(':','').isdigit()
works well for checking if x is a ratio.
x.replace('/','',1).isdigit()
works well for checking if x is a fraction.
def is_number(n):try:
float(n)# Type-casting the string to `float`.# If string is not a valid `float`, # it'll raise `ValueError` exceptionexceptValueError:returnFalsereturnTrue
# `nan_num` variable is taken from above example>>> nan_num == nan_num
False
因此,上述功能is_number可以更新,返回False的"NaN"是:
def is_number(n):
is_number =Truetry:
num = float(n)# check for "nan" floats
is_number = num == num # or use `math.isnan(num)`exceptValueError:
is_number =Falsereturn is_number
样品运行:
>>> is_number('Nan')# not a number "Nan" stringFalse>>> is_number('nan')# not a number string "nan" with all lower casedFalse>>> is_number('123')# positive integerTrue>>> is_number('-123')# negative integerTrue>>> is_number('-1.12')# negative `float`True>>> is_number('abc')# "some random" stringFalse
This answer provides step by step guide having function with examples to find the string is:
Positive integer
Positive/negative – integer/float
How to discard “NaN” (not a number) strings while checking for number?
Check if string is positive integer
You may use str.isdigit() to check whether given string is positive integer.
Sample Results:
# For digit
>>> '1'.isdigit()
True
>>> '1'.isalpha()
False
Check for string as positive/negative – integer/float
str.isdigit() returns False if the string is a negative number or a float number. For example:
# returns `False` for float
>>> '123.3'.isdigit()
False
# returns `False` for negative number
>>> '-123'.isdigit()
False
If you want to also check for the negative integers and float, then you may write a custom function to check for it as:
def is_number(n):
try:
float(n) # Type-casting the string to `float`.
# If string is not a valid `float`,
# it'll raise `ValueError` exception
except ValueError:
return False
return True
Sample Run:
>>> is_number('123') # positive integer number
True
>>> is_number('123.4') # positive float number
True
>>> is_number('-123') # negative integer number
True
>>> is_number('-123.4') # negative `float` number
True
>>> is_number('abc') # `False` for "some random" string
False
Discard “NaN” (not a number) strings while checking for number
The above functions will return True for the “NAN” (Not a number) string because for Python it is valid float representing it is not a number. For example:
>>> is_number('NaN')
True
In order to check whether the number is “NaN”, you may use math.isnan() as:
>>> import math
>>> nan_num = float('nan')
>>> math.isnan(nan_num)
True
Or if you don’t want to import additional library to check this, then you may simply check it via comparing it with itself using ==. Python returns False when nan float is compared with itself. For example:
# `nan_num` variable is taken from above example
>>> nan_num == nan_num
False
Hence, above function is_number can be updated to return False for "NaN" as:
def is_number(n):
is_number = True
try:
num = float(n)
# check for "nan" floats
is_number = num == num # or use `math.isnan(num)`
except ValueError:
is_number = False
return is_number
Sample Run:
>>> is_number('Nan') # not a number "Nan" string
False
>>> is_number('nan') # not a number string "nan" with all lower cased
False
>>> is_number('123') # positive integer
True
>>> is_number('-123') # negative integer
True
>>> is_number('-1.12') # negative `float`
True
>>> is_number('abc') # "some random" string
False
PS: Each operation for each check depending on the type of number comes with additional overhead. Choose the version of is_number function which fits your requirement.
Casting to float and catching ValueError is probably the fastest way, since float() is specifically meant for just that. Anything else that requires string parsing (regex, etc) will likely be slower due to the fact that it’s not tuned for this operation. My $0.02.
回答 13
您可以使用Unicode字符串,它们有一种方法可以执行您想要的操作:
>>> s = u"345">>> s.isnumeric()True
要么:
>>> s ="345">>> u = unicode(s)>>> u.isnumeric()True
import time, re, random, string
ITERATIONS =10000000classTimer:def __enter__(self):
self.start = time.clock()return self
def __exit__(self,*args):
self.end = time.clock()
self.interval = self.end - self.start
def check_regexp(x):return re.compile("^\d*\.?\d*$").match(x)isnotNonedef check_replace(x):return x.replace('.','',1).isdigit()def check_exception(s):try:
float(s)returnTrueexceptValueError:returnFalse
to_check =[check_regexp, check_replace, check_exception]print('preparing data...')
good_numbers =[
str(random.random()/ random.random())for x in range(ITERATIONS)]
bad_numbers =['.'+ x for x in good_numbers]
strings =[''.join(random.choice(string.ascii_uppercase + string.digits)for _ in range(random.randint(1,10)))for x in range(ITERATIONS)]print('running test...')for func in to_check:withTimer()as t:for x in good_numbers:
res = func(x)print('%s with good floats: %s'%(func.__name__, t.interval))withTimer()as t:for x in bad_numbers:
res = func(x)print('%s with bad floats: %s'%(func.__name__, t.interval))withTimer()as t:for x in strings:
res = func(x)print('%s with strings: %s'%(func.__name__, t.interval))
以下是2017年MacBook Pro 13上Python 2.7.10的结果:
check_regexp with good floats:12.688639
check_regexp with bad floats:11.624862
check_regexp with strings:11.349414
check_replace with good floats:4.419841
check_replace with bad floats:4.294909
check_replace with strings:4.086358
check_exception with good floats:3.276668
check_exception with bad floats:13.843092
check_exception with strings:15.786169
以下是2017年MacBook Pro 13上Python 3.6.5的结果:
check_regexp with good floats:13.472906000000009
check_regexp with bad floats:12.977665000000016
check_regexp with strings:12.417542999999995
check_replace with good floats:6.011045999999993
check_replace with bad floats:4.849356
check_replace with strings:4.282754000000011
check_exception with good floats:6.039081999999979
check_exception with bad floats:9.322753000000006
check_exception with strings:9.952595000000002
以下是2017年MacBook Pro 13上PyPy 2.7.13的结果:
check_regexp with good floats:2.693217
check_regexp with bad floats:2.744819
check_regexp with strings:2.532414
check_replace with good floats:0.604367
check_replace with bad floats:0.538169
check_replace with strings:0.598664
check_exception with good floats:1.944103
check_exception with bad floats:2.449182
check_exception with strings:2.200056
I wanted to see which method is fastest. Overall the best and most consistent results were given by the check_replace function. The fastest results were given by the check_exception function, but only if there was no exception fired – meaning its code is the most efficient, but the overhead of throwing an exception is quite large.
Please note that checking for a successful cast is the only method which is accurate, for example, this works with check_exception but the other two test functions will return False for a valid float:
huge_number = float('1e+100')
Here is the benchmark code:
import time, re, random, string
ITERATIONS = 10000000
class Timer:
def __enter__(self):
self.start = time.clock()
return self
def __exit__(self, *args):
self.end = time.clock()
self.interval = self.end - self.start
def check_regexp(x):
return re.compile("^\d*\.?\d*$").match(x) is not None
def check_replace(x):
return x.replace('.','',1).isdigit()
def check_exception(s):
try:
float(s)
return True
except ValueError:
return False
to_check = [check_regexp, check_replace, check_exception]
print('preparing data...')
good_numbers = [
str(random.random() / random.random())
for x in range(ITERATIONS)]
bad_numbers = ['.' + x for x in good_numbers]
strings = [
''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(random.randint(1,10)))
for x in range(ITERATIONS)]
print('running test...')
for func in to_check:
with Timer() as t:
for x in good_numbers:
res = func(x)
print('%s with good floats: %s' % (func.__name__, t.interval))
with Timer() as t:
for x in bad_numbers:
res = func(x)
print('%s with bad floats: %s' % (func.__name__, t.interval))
with Timer() as t:
for x in strings:
res = func(x)
print('%s with strings: %s' % (func.__name__, t.interval))
Here are the results with Python 2.7.10 on a 2017 MacBook Pro 13:
check_regexp with good floats: 12.688639
check_regexp with bad floats: 11.624862
check_regexp with strings: 11.349414
check_replace with good floats: 4.419841
check_replace with bad floats: 4.294909
check_replace with strings: 4.086358
check_exception with good floats: 3.276668
check_exception with bad floats: 13.843092
check_exception with strings: 15.786169
Here are the results with Python 3.6.5 on a 2017 MacBook Pro 13:
check_regexp with good floats: 13.472906000000009
check_regexp with bad floats: 12.977665000000016
check_regexp with strings: 12.417542999999995
check_replace with good floats: 6.011045999999993
check_replace with bad floats: 4.849356
check_replace with strings: 4.282754000000011
check_exception with good floats: 6.039081999999979
check_exception with bad floats: 9.322753000000006
check_exception with strings: 9.952595000000002
Here are the results with PyPy 2.7.13 on a 2017 MacBook Pro 13:
check_regexp with good floats: 2.693217
check_regexp with bad floats: 2.744819
check_regexp with strings: 2.532414
check_replace with good floats: 0.604367
check_replace with bad floats: 0.538169
check_replace with strings: 0.598664
check_exception with good floats: 1.944103
check_exception with bad floats: 2.449182
check_exception with strings: 2.200056
def is_number(s):try:
n=str(float(s))if n =="nan"or n=="inf"or n=="-inf":returnFalseexceptValueError:try:
complex(s)# for complexexceptValueError:returnFalsereturnTrue
>>> a=454>>> a.isdigit()Traceback(most recent call last):File"<stdin>", line 1,in<module>AttributeError:'int' object has no attribute 'isdigit'>>> a="454">>> a.isdigit()True
Finds whether the given variable is numeric. Numeric strings consist of optional sign, any number of digits, optional decimal part and optional exponential part. Thus +0123.45e6 is a valid numeric value. Hexadecimal (e.g. 0xf4c3b00c) and binary (e.g. 0b10100111001) notation is not allowed.
is_numeric function
import ast
import numbers
def is_numeric(obj):
if isinstance(obj, numbers.Number):
return True
elif isinstance(obj, str):
nodes = list(ast.walk(ast.parse(obj)))[1:]
if not isinstance(nodes[0], ast.Expr):
return False
if not isinstance(nodes[-1], ast.Num):
return False
nodes = nodes[1:-1]
for i in range(len(nodes)):
#if used + or - in digit :
if i % 2 == 0:
if not isinstance(nodes[i], ast.UnaryOp):
return False
else:
if not isinstance(nodes[i], (ast.USub, ast.UAdd)):
return False
return True
else:
return False
Finds whether the given variable is float. float strings consist of optional sign, any number of digits, …
import ast
def is_float(obj):
if isinstance(obj, float):
return True
if isinstance(obj, int):
return False
elif isinstance(obj, str):
nodes = list(ast.walk(ast.parse(obj)))[1:]
if not isinstance(nodes[0], ast.Expr):
return False
if not isinstance(nodes[-1], ast.Num):
return False
if not isinstance(nodes[-1].n, float):
return False
nodes = nodes[1:-1]
for i in range(len(nodes)):
if i % 2 == 0:
if not isinstance(nodes[i], ast.UnaryOp):
return False
else:
if not isinstance(nodes[i], (ast.USub, ast.UAdd)):
return False
return True
else:
return False
I did some speed test. Lets say that if the string is likely to be a number the try/except strategy is the fastest possible.If the string is not likely to be a number and you are interested in Integer check, it worths to do some test (isdigit plus heading ‘-‘).
If you are interested to check float number, you have to use the try/except code whitout escape.
def str_to_type (s):""" Get possible cast type for a string
Parameters
----------
s : string
Returns
-------
float,int,str,bool : type
Depending on what it can be cast to
"""try:
f = float(s)if"."notin s:return int
return float
exceptValueError:
value = s.upper()if value =="TRUE"or value =="FALSE":return bool
return type(s)
例
str_to_type("true")# bool
str_to_type("6.0")# float
str_to_type("6")# int
str_to_type("6abc")# str
str_to_type(u"6abc")# unicode
您可以捕获类型并使用它
s ="6.0"
type_ = str_to_type(s)# float
f = type_(s)
I needed to determine if a string cast into basic types (float,int,str,bool). After not finding anything on the internet I created this:
def str_to_type (s):
""" Get possible cast type for a string
Parameters
----------
s : string
Returns
-------
float,int,str,bool : type
Depending on what it can be cast to
"""
try:
f = float(s)
if "." not in s:
return int
return float
except ValueError:
value = s.upper()
if value == "TRUE" or value == "FALSE":
return bool
return type(s)
If you want to return False for a NaN and Inf, change line to x = float(s); return (x == x) and (x – 1 != x). This should return True for all floats except Inf and NaN
But this doesn’t quite work, because for sufficiently large floats, x-1 == x returns true. For example, 2.0**54 - 1 == 2.0**54
check_regexp with good floats:18.001921
check_regexp with bad floats:17.861423
check_regexp with strings:17.558862
check_correct_regexp with good floats:11.04428
check_correct_regexp with bad floats:8.71211
check_correct_regexp with strings:8.144161
check_replace with good floats:6.020597
check_replace with bad floats:5.343049
check_replace with strings:5.091642
check_exception with good floats:5.201605
check_exception with bad floats:23.921864
check_exception with strings:23.755481
I think your solution is fine, but there is a correct regexp implementation.
There does seem to be a lot of regexp hate towards these answers which I think is unjustified, regexps can be reasonably clean and correct and fast. It really depends on what you’re trying to do. The original question was how can you “check if a string can be represented as a number (float)” (as per your title). Presumably you would want to use the numeric/float value once you’ve checked that it’s valid, in which case your try/except makes a lot of sense. But if, for some reason, you just want to validate that a string is a number then a regex also works fine, but it’s hard to get correct. I think most of the regex answers so far, for example, do not properly parse strings without an integer part (such as “.7”) which is a float as far as python is concerned. And that’s slightly tricky to check for in a single regex where the fractional portion is not required. I’ve included two regex to show this.
It does raise the interesting question as to what a “number” is. Do you include “inf” which is valid as a float in python? Or do you include numbers that are “numbers” but maybe can’t be represented in python (such as numbers that are larger than the float max).
There’s also ambiguities in how you parse numbers. For example, what about “–20”? Is this a “number”? Is this a legal way to represent “20”? Python will let you do “var = –20” and set it to 20 (though really this is because it treats it as an expression), but float(“–20”) does not work.
Anyways, without more info, here’s a regex that I believe covers all the ints and floats as python parses them.
# Doesn't properly handle floats missing the integer part, such as ".7"
SIMPLE_FLOAT_REGEXP = re.compile(r'^[-+]?[0-9]+\.?[0-9]+([eE][-+]?[0-9]+)?$')
# Example "-12.34E+56" # sign (-)
# integer (12)
# mantissa (34)
# exponent (E+56)
# Should handle all floats
FLOAT_REGEXP = re.compile(r'^[-+]?([0-9]+|[0-9]*\.[0-9]+)([eE][-+]?[0-9]+)?$')
# Example "-12.34E+56" # sign (-)
# integer (12)
# OR
# int/mantissa (12.34)
# exponent (E+56)
def is_float(str):
return True if FLOAT_REGEXP.match(str) else False
Running the benchmarking code in @ron-reiter’s answer shows that this regex is actually faster than the normal regex and is much faster at handling bad values than the exception, which makes some sense. Results:
check_regexp with good floats: 18.001921
check_regexp with bad floats: 17.861423
check_regexp with strings: 17.558862
check_correct_regexp with good floats: 11.04428
check_correct_regexp with bad floats: 8.71211
check_correct_regexp with strings: 8.144161
check_replace with good floats: 6.020597
check_replace with bad floats: 5.343049
check_replace with strings: 5.091642
check_exception with good floats: 5.201605
check_exception with bad floats: 23.921864
check_exception with strings: 23.755481
回答 21
import re
def is_number(num):
pattern = re.compile(r'^[-+]?[-0-9]\d*\.\d*|[-+]?\.?[0-9]\d*$')
result = pattern.match(num)if result:returnTrueelse:returnFalse>>>: is_number('1')True>>>: is_number('111')True>>>: is_number('11.1')True>>>: is_number('-11.1')True>>>: is_number('inf')False>>>: is_number('-inf')False
Replace the myvar.apppend with whatever operation you want to do with the string if it turns out to be a number. The idea is to try to use a float() operation and use the returned error to determine whether or not the string is a number.
I also used the function you mentioned, but soon I notice that strings as “Nan”, “Inf” and it’s variation are considered as number. So I propose you improved version of your function, that will return false on those type of input and will not fail “1e3” variants:
def is_float(text):
try:
float(text)
# check for nan/infinity etc.
if text.isalpha():
return False
return True
except ValueError:
return False
import sys
def fix_quotes(s):try:
float(s)return s
exceptValueError:return'"{0}"'.format(s)for line in sys.stdin:
input = line.split()print input[0],'<- c(',','.join(fix_quotes(c)for c in input[1:]),')'
You can generalize the exception technique in a useful way by returning more useful values than True and False. For example this function puts quotes round strings but leaves numbers alone. Which is just what I needed for a quick and dirty filter to make some variable definitions for R.
import sys
def fix_quotes(s):
try:
float(s)
return s
except ValueError:
return '"{0}"'.format(s)
for line in sys.stdin:
input = line.split()
print input[0], '<- c(', ','.join(fix_quotes(c) for c in input[1:]), ')'
I was working on a problem that led me to this thread, namely how to convert a collection of data to strings and numbers in the most intuitive way. I realized after reading the original code that what I needed was different in two ways:
1 – I wanted an integer result if the string represented an integer
2 – I wanted a number or a string result to stick into a data structure
so I adapted the original code to produce this derivative:
def string_or_number(s):
try:
z = int(s)
return z
except ValueError:
try:
z = float(s)
return z
except ValueError:
return s
回答 28
尝试这个。
def is_number(var):try:if var == int(var):returnTrueexceptException:returnFalse
def is_number(var):
try:
if var == int(var):
return True
except Exception:
return False
回答 29
def is_float(s):if s isNone:returnFalseif len(s)==0:returnFalse
digits_count =0
dots_count =0
signs_count =0for c in s:if'0'<= c <='9':
digits_count +=1elif c =='.':
dots_count +=1elif c =='-'or c =='+':
signs_count +=1else:returnFalseif digits_count ==0:returnFalseif dots_count >1:returnFalseif signs_count >1:returnFalsereturnTrue
def is_float(s):
if s is None:
return False
if len(s) == 0:
return False
digits_count = 0
dots_count = 0
signs_count = 0
for c in s:
if '0' <= c <= '9':
digits_count += 1
elif c == '.':
dots_count += 1
elif c == '-' or c == '+':
signs_count += 1
else:
return False
if digits_count == 0:
return False
if dots_count > 1:
return False
if signs_count > 1:
return False
return True