Вы можете использовать одно регулярное выражение:
file = open('FilePath/OUTPUT.01')
lines = file.read()
file.close()
with open("output.txt","w") as f:
for match in re.finditer(r"(?m)^\s*-+\s+\S+\s+(-?[\d.]+E[+-]\d+)", lines):
f.write(match.group(1)+"\n")
Это должно записать все вторые числа, которые появляются после строки, которая полностью состоит из -
, в файл output.txt
.
В этом регулярном выражении предполагается, что столбцы разделены пробелами и что первый столбец никогда не будет пустым.
Объяснение:
(?m) # allow ^ to match at start of line, not just start of string
^ # anchor the search at the start of the line
\s* # match any leading whitespace
-+ # match one or more dashes
\s+ # match trailing whitespace, including linebreak characters
\S+ # match a run of non-whitespace characters (we're now one line ahead of the dashes
\s+ # match a run of whitespace
(-?[\d.]+E[+-]\d+) # match a number in scientific notation