我有一个具有以下格式的日志文件。我只使用了一小部分来突出我在使用Python的正则表达式提取相关信息时所遇到的问题。
Time = 1
smoothSolver: Solving for Ux, Initial residual = 0.230812, Final residual = 0.0134171, No Iterations 2
smoothSolver: Solving for Uy, Initial residual = 0.283614, Final residual = 0.0158797, No Iterations 3
smoothSolver: Solving for Uz, Initial residual = 0.190444, Final residual = 0.016567, No Iterations 2
GAMG: Solving for p, Initial residual = 0.0850116, Final residual = 0.00375608, No Iterations 3
time step continuity errors : sum local = 0.00999678, global = 0.00142109, cumulative = 0.00142109
smoothSolver: Solving for omega, Initial residual = 0.00267604, Final residual = 0.000166675, No Iterations 3
bounding omega, min: -26.6597 max: 18468.7 average: 219.43
smoothSolver: Solving for k, Initial residual = 1, Final residual = 0.0862096, No Iterations 2
ExecutionTime = 4.84 s ClockTime = 5 s
Time = 2
smoothSolver: Solving for Ux, Initial residual = 0.0299872, Final residual = 0.00230507, No Iterations 2
smoothSolver: Solving for Uy, Initial residual = 0.145767, Final residual = 0.00882969, No Iterations 3
smoothSolver: Solving for Uz, Initial residual = 0.0863129, Final residual = 0.00858536, No Iterations 2
GAMG: Solving for p, Initial residual = 0.394189, Final residual = 0.0175138, No Iterations 3
time step continuity errors : sum local = 0.00862823, global = 0.00212477, cumulative = 0.00354587
smoothSolver: Solving for omega, Initial residual = 0.00258475, Final residual = 0.000222705, No Iterations 3
smoothSolver: Solving for k, Initial residual = 0.112805, Final residual = 0.0054572, No Iterations 3
ExecutionTime = 5.9 s ClockTime = 6 s
Time = 3
smoothSolver: Solving for Ux, Initial residual = 0.128298, Final residual = 0.0070293, No Iterations 2
smoothSolver: Solving for Uy, Initial residual = 0.138825, Final residual = 0.0116437, No Iterations 3
smoothSolver: Solving for Uz, Initial residual = 0.0798979, Final residual = 0.00491246, No Iterations 3
GAMG: Solving for p, Initial residual = 0.108748, Final residual = 0.00429273, No Iterations 2
time step continuity errors : sum local = 0.0073211, global = -0.00187909, cumulative = 0.00166678
smoothSolver: Solving for omega, Initial residual = 0.00238456, Final residual = 0.000224435, No Iterations 3
smoothSolver: Solving for k, Initial residual = 0.0529661, Final residual = 0.00280851, No Iterations 3
ExecutionTime = 6.92 s ClockTime = 7 s
我的代码如下:
# Opening the log file for reading
with open(logFile, 'r') as logfile_read:
for line in logfile_read:
line = line.rstrip()
# To extract Time or iteration
if 'Time' in line:
iteration_time = re.findall(r'^Time\s+=\s+(.*)', line)
# To extract local, global and cumulative values
if 'local' in line:
local_global_cumu = re.findall(r'sum\s+local\s+=\s+(.*),\s+global\s+=\s+(.*),\s+cumulative\s+=\s+(.*)', line)
if local_global_cumu:
local_global_cumu = local_global_cumu[0]
(cont_Local, cont_Global, cont_Cumulative) = local_global_cumu
for t in iteration_time:
contLocal.write("%s\t%s\n" %(t, cont_Local))
contGlobal.write("%s\t%s\n" %(t, cont_Global))
contCumulative.write("%s\t%s\n" %(t, cont_Cumulative))
# To extract kinetic energy residual values
if 'k,' in line:
kinetic_energy = re.findall(r'k,\s+Initial\s+residual\s+=\s+(.*),\s+Final\s+residual\s+=\s+(.*),\s+No\s+Iterations\s+(.*)', line)
if kinetic_energy:
kinetic_energy = kinetic_energy[0]
(k_initial, k_FinalRes, k_Iters) = kinetic_energy
for t in iteration_time:
k.write("%s\t%s\n" %(t, k_initial))
kFinalRes.write("%s\t%s\n" %(t, k_FinalRes))
kIters.write("%s\t%s\n" %(t, k_Iters))
# To extract omega residual values
if 'omega,' in line:
omega_values = re.findall(r'omega,\s+Initial\s+residual\s+=\s+(.*),\s+Final\s+residual\s+=\s+(.*),\s+No\s+Iterations\s+(.*)', line)
if omega_values:
omega_rate = omega_values[0]
(omega_initial, omega_FinalRes, omega_Iters) = omega_rate
for t in iteration_time:
print ("%s\t%s\n" %(t, omega_initial))
最后一部分(要提取omega残差值),omega_initial
正在随机打印重复值。 print ("%s\t%s\n" %(t, omega_initial))
的输出如下所示:
1 0.00267604
1 0.00267604
2 0.00258475
3 0.00238456
我无法理解为什么第一组值被写入两次,而日志文件中只有一个这样的值被读取。处理完整日志文件时,会对许多值随机发生此类复制。
对于代码中存在的任何先前变量,未观察到此行为。
答案 0 :(得分:0)
我能够弄清楚为什么我会得到重复的值。在我的日志文件中,我有两个omega
实例,因此根据我的理解,它提取了两次。诀窍是这个。我现在if 'omega,' in line:
而不是if 'Solving for omega,' in line:
,这解决了问题。
if 'Solving for omega,' in line:
omega_values = re.findall(r'omega,\s+Initial\s+residual\s+=\s+(.*),\s+Final\s+residual\s+=\s+(.*),\s+No\s+Iterations\s+(.*)', line)
if omega_values:
omega_rate = omega_values[0]
(omega_initial, omega_FinalRes, omega_Iters) = omega_rate
for t in iteration_time:
print ("%s\t%s\n" %(t, omega_initial))