Using Regex to match numbers on rows of different size in Python -
i have file contain positive , negative numbers in row of different sizes. trying extract numbers using regex.however, skips rows below.
part of input file:
. . . ...s -- -0.28096 -0.27907 -0.27770 -0.27730 -0.27573 ...s -- -0.27149 -0.27076 -0.27036 -0.26883 -0.26794 ...s -- -0.26301 -0.26114 -0.26098 -0.25950 -0.25891 ...s -- -0.25536 -0.25209 -0.24952 -0.24903 -0.24533 ...s -- **-0.24351 -0.23272 -0.07408** ...s -- -0.01149 -0.01028 -0.00892 -0.00888 -0.00665 ...s -- -0.00445 -0.00268 -0.00006 **0.00109 0.00187** ...s -- **0.00295 0.00318 0.00470 0.00575 0.00696** . . .
my code:
with open('input') x: file.write('output') file.write("\n") t in itertools.islice(x,7821,7831): k = re.search(r'(?<=s\s\s\s\s\s\s)[+-]?\d+\.\d+|\d+\s\s\[-+]?\d+\.\d+|\d+\s\s\[-+]?\d+\.\d+|\d+\s\s\[-+]?\d+\.\d+|\d+\s\s\[-+]?\d+\.\d+|\d+' , t) if k: r1.append(k.group()) file.write(str(' '.join(map(str,r1))))
the output
output
-0.28096 -0.27907 -0.27770 -0.27730 -0.27573 -0.27149 -0.27076 -0.27036 -0.26883 -0.26794 -0.26301 -0.26114 -0.26098 -0.25950 -0.25891 -0.25536 -0.25209 -0.24952 -0.24903 -0.24533 -0.01149 -0.01028 -0.00892 -0.00888 -0.00665
as can see output not contain numbers in bold in input file.
how should modified code make more inclusive , extract data between lines put in range? thank in advance!
your regex allows negative numbers, , requires @ least 5 numbers in line.
try (?<=s\s\s\s\s)(\s\s[- ]\d+\.\d+)+
.
Comments
Post a Comment