如何打印出re.compile()找到行后的那一行

0 投票
3 回答
3566 浏览
提问于 2025-04-16 00:15

使用这段代码

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
step
-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
terminating.
eng_tot
-1.1274E+05
3D

我该如何将这些数据写入一个文件(Tmp.txt)呢?现在它只写入了最后一行'3D'。另外,我想去掉所有不是以x.xxxxExxx形式出现的行(也就是只保留数字)。

3 个回答

0

我觉得用正则表达式来解决这个问题没必要。你可以试试下面这个方法:

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

i 是一个索引,用来表示 linelines 中的位置,所以 i+1 就是下一行的位置:

print lines[i+1]

要确保 ---- 不是最后一行,否则程序会尝试读取一个不存在的位置。另外,你的正则表达式 \s+-+\s+ 要求在 - 的前后都有空格,因为 \s+ 表示一个或多个空格;你可能想用 \s*

2

你可以用一个简单的正则表达式:

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

撰写回答