使用 Python 从文本文件中跳过行

hyp*_*ics 0 python regex

我正在处理一个非常大的日志文件以使用 Python 正则表达式提取信息。但是,我只想在找到特定字符串(在本例中为Starting time loop. 日志文件的最小版本如下:

Pstream initialized with:
floatTransfer      : 0
nProcsSimpleSum    : 0
commsType          : nonBlocking
polling iterations : 0
sigFpe : Enabling floating point exception trapping (FOAM_SIGFPE).
fileModificationChecking : Monitoring run-time modified files using timeStampMaster
allowSystemOperations : Disallowing user-supplied system call operations

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
Create time

Create mesh for time = 0


PIMPLE: Operating solver in PISO mode


Reading g

Reading relaxProperties
Reading field p_rgh

Reading field alpha1

Reading field Urel

Reading/calculating face flux field phi

Reading transportProperties

Selecting incompressible transport model Newtonian
Selecting incompressible transport model Newtonian
Selecting turbulence model type LESModel
Selecting LES turbulence model Smagorinsky
Selecting LES delta type vanDriest
Selecting LES delta type cubeRootVol
SmagorinskyCoeffs
{
   ce              1.048;
   ck              0.02;
}

Reading STFProperties

Calculating field g.h

time step continuity errors : sum local = 8.4072346e-06, global = -1.5271655e-21, cumulative = -1.5271655e-21
GAMGPCG:  Solving for pcorr, Initial residual = 1, Final residual = 4.7194845e-06, No Iterations 9
GAMGPCG:  Solving for pcorr, Initial residual = 0.13716381, Final residual = 2.9068099e-06, No Iterations 6
time step continuity errors : sum local = 1.3456802e-10, global = -6.7890391e-13, cumulative = -6.7890392e-13
Courant Number mean: 0.021611246 max: 0.39023401
fieldAverage fieldAverage1:
Starting averaging at time 0


Starting time loop

Courant Number mean: 0.02156811 max: 0.3894551
Interface Courant Number mean: 0 max: 0
deltaT = 0.00022522523
Time = 0.000225225
Run Code Online (Sandbox Code Playgroud)

目前,测试脚本如下:

logf = open(logName, 'r')
p = logf.tell()
logf.seek(0, 0)
for l in logf:
    if l.startswith('Starting time loop'):
        print l
Run Code Online (Sandbox Code Playgroud)

但是,print l打印日志文件中的所有行。请注意,日志文件打开为logf

geo*_*org 5

python迭代器(文件对象所属的)的好处是它们保持状态,所以如果你有两个for循环,当第一个循环停止时,第二个循环开始。这导致了以下常规模式:

for line in logf:
   if <some condition>
       break

for line in logf:
   process lines after that one
Run Code Online (Sandbox Code Playgroud)

另一种更简洁的方法是itertools.dropwhile.