问题:
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
更加糟糕。
解决方案:
参考一: https://stackoom.com/question/1U6I/如何检查字符串是否为数字-浮点数参考二: https://oldbug.net/q/1U6I/How-do-I-check-if-a-string-is-a-number-float
来源:oschina
链接:https://my.oschina.net/stackoom/blog/3217715