Check if a string contains a number

后端 未结 16 1367
無奈伤痛
無奈伤痛 2020-11-22 11:14

Most of the questions I\'ve found are biased on the fact they\'re looking for letters in their numbers, whereas I\'m looking for numbers in what I\'d like to be a numberless

16条回答
  •  不思量自难忘°
    2020-11-22 11:43

    What about this one?

    import string
    
    def containsNumber(line):
        res = False
        try:
            for val in line.split():
                if (float(val.strip(string.punctuation))):
                    res = True
                    break
        except ValueError:
            pass
        return res
    
    containsNumber('234.12 a22') # returns True
    containsNumber('234.12L a22') # returns False
    containsNumber('234.12, a22') # returns True
    

提交回复
热议问题