Как распечатать строку после строки, найденной в re.compile () - PullRequest
0 голосов
/ 21 июня 2010

Используя этот код

import re
file = open('FilePath/OUTPUT.01')
lines = file.read()
file.close()
for match in re.finditer(r"(?m)^\s*-+\s+\S+\s+(\S+)", lines):
eng = match.group(1)
open('Tmp.txt', 'w').writelines(eng)
print match.group(1)

Я получаю столбец данных, который выглядит следующим образом:

-1.1266E + 05
-1.1265E + 05
-1.1265E + 05
-1.1265E + 05
-1.1264E + 05
-1.1264E + 05
-1.1264E + 05
-1.1263E + 05
шаг
-1.1263E + 05
-1.1262E + 05
-1.1262E + 05
-1.1261E + 05
-1.1261E + 05
-1.1260E + 05
-1.1260E + 05
-1.1259E + 05
step
-1.1259E + 05
-1.1258E + 05
-1.1258E + 05
-1.1258E + 05
-1.1257E + 05
завершается.
eng_tot
-1.1274E + 05
3D

Как мне записать файл (Tmp.txt)?На данный момент записывается только последняя строка «3D».Также я хотел бы исключить все строки, которые не имеют форму x.xxxxExxx (т.е. только цифры).

Ответы [ 3 ]

2 голосов
/ 21 июня 2010

Вы можете использовать одно регулярное выражение:

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
0 голосов
/ 21 июня 2010

Я бы не стал беспокоиться об этом.Попробуйте выполнить следующее:

output = file("tmp.txt", "w")        # open a file for writing
flagged = False                      # when 'flagged == True' we will print the line
for line in file("FilePath/OUTPUT.01"):
    if flagged:
        try:
            result = line.split()[1] # python is zero-indexed!
            print>>output, result    # print to output only if the split worked
        except IndexError:           # otherwise do nothing
            pass
        flagged = False              # but reset the flag
    else:
        if set(line.strip()) == set(["-"]): # does the line consist only of '-'?
            flagged = True           # if so, set the flag to print the next line

Вот версия, которая позволяет указать число смещений строк и номер столбца:

OFFSET = 3 # the third line after the `----`
COLUMN = 2 # column index 2

output = file("tmp.txt", "w")
counter = 0                           # 0 evaluates as False
for line in file("FilePath/OUTPUT.01"):
    if counter:                       # any non-zero value evaluates as True
        if counter == OFFSET:
            try:
                result = line.split()[COLUMN] 
                print>>output, result # print to output only if the split worked
            except IndexError:        # otherwise do nothing
                pass
            counter = 0               # reset the flag once you've reached the OFFSET line
        else:
            counter += 1
    else:
        if set(line.strip()) == set(["-"]): # does the line consist only of '-'?
            counter = 1
0 голосов
/ 21 июня 2010

i - это индекс в lines, в котором находится line, поэтому i+1 - следующая строка:

print lines[i+1]

Убедитесь, что ---- не последняя строка, или она будет пытаться читать из несуществующего местоположения. Кроме того, ваше регулярное выражение \s+-+\s+ требует наличия пробелов до и после - s, так как \s+ означает 1 или более пробелов; Вы, вероятно, имели в виду \s*

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...