开发者

How to print out the line after the line found in re.compile()

Using this code

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)

I get a column of data that looks like this:

-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

How do I write it a file (Tmp.txt)? As of now it only writes the last line '3D'. Also I'd like to eliminate all开发者_JAVA技巧 the lines that aren't of the form x.xxxxExxx (i.e. just the numbers).


You could use a single regex:

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")

This should write all the second numbers that occur after a line that consists entirely of - into the file output.txt.

This regex assumes that the columns are space-separated, and that the first column will never be empty.

Explanation:

(?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


i is the index into lines that line is at, so i+1 is the next line:

print lines[i+1]

Make sure the ---- isn't the last line or this will try to read from a location that doesn't exist. Also, your regular expression \s+-+\s+ requires that there be spaces before and after the -s, as \s+ means 1 or more spaces; you probably meant \s*


I wouldn't bother with REs for this. Try the following:

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

Here's a version which allows you to specify the number of lines offset, and the column number:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜