What is the best possible way to check if a string can be represented as a number in Python?
(检查字符串是否可以在Python中表示为数字的最佳方法是什么?)
The function I currently have right now is:
(我目前拥有的功能是:)
def is_number(s):
try:
float(s)
return True
except ValueError:
return False
Which, not only is ugly and slow, seems clunky.
(不仅丑陋且缓慢,而且看起来笨拙。)
However I haven't found a better method because calling float
in the main function is even worse. (但是我还没有找到更好的方法,因为在main函数中调用float
更加糟糕。)
ask by Daniel Goldberg translate from so 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…