How do I find one number in a string in Python?

前端 未结 5 2245
感动是毒
感动是毒 2021-02-20 02:11

I have a file called something like FILE-1.txt or FILE-340.txt. I want to be able to get the number from the file name. I\'ve found that I can use

numbers = re.f         


        
5条回答
  •  囚心锁ツ
    2021-02-20 02:48

    Use search instead of findall:

    number = re.search(r'\d+', filename).group()
    

    Alternatively:

    number = filter(str.isdigit, filename)
    

提交回复
热议问题