I have files that contain both strings and floats. I am interested in finding the floats after a specific string. Any help in writing such a function that reads the file look for that specific string and returns the float after it will be much appreciated.
Thanks
An example of a file is
lines = """aaaaaaaaaaaaaaa bbbbbbbbbbbbbbb cccccccccc
qq vvv rrr ssssa 22.6
zzzzx bbbb 12.0
xxxxxxxxxx -1.099
zzzz bbb nnn 33.5"""
import re
lines = """aaaaaaaaaaaaaaa bbbbbbbbbbbbbbb cccccccccc
qq vvv rrr ssssa 22.6
zzzzx bbbb 12.0
xxxxxxxxxx -1.099
zzzz bbb nnn 33.5"""
str_to_search = 'xxxxxxxxxx'
num = re.findall(r'^' + str_to_search + r' (\d+\.\d+)', lines, flags=re.M)
print(num)
This works if there are no negative signs. In other words, if the number after the string 'xxxxxxxxxx' is 1.099 rather than '-1.099', it works fine. The question I have is how to generalize so it accounts for negative numbers as well given that it can be positive number (no sign in this case) or a negative number (with a negative sign in this case)