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_file.txt', 'r')
file = file.readlines()
for line in file:
line=re.findall(r'\d+|\d+.\d+', line)
print line
Maybe this will help.
The
string
here can be yourline
. I just put in dummy text.Spits out the following when executed:
Experiment
I performed a little experiment on the part corpus of Frankenstein.
Note I use
.read()
to read the entire file instead of line by line processing.This was the result:
Unit Testing
I wrote a lighter version that works with your string supplied.
When things pass, this gives a green signal, as shown below:
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 numbersWithout clarification, you can try the following.