How to extract only numbers from input file. Numbers can be float/int

后端 未结 3 622
清歌不尽
清歌不尽 2021-01-29 13:42

I want to extract numbers(integers and float) from a file(exclude all special symbols and alphabets). Numbers from all positions.

import re
file = open(\'input_f         


        
3条回答
  •  醉话见心
    2021-01-29 14:30

    re.findall("[+-]?\d+\.?\d*",some_text)
    

    I think at least

    [+-]? zero or one of either + or - (ie optional)

    \d+ one or more digits

    \.? optionally a decimal

    \d* zero or more additional numbers

提交回复
热议问题