У меня есть файл журнала, имеющий следующий формат. Я включил только небольшую часть, чтобы выделить проблему, которая возникает у меня, когда я использую регулярное выражение Python для извлечения некоторой релевантной информации.Python regex output случайное дублирование
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_initial
печатает случайным образом дублируют значения. Выход print ("%s\t%s\n" %(t, omega_initial))
показан ниже:
1 0.00267604
1 0.00267604
2 0.00258475
3 0.00238456
Я не могу понять, почему первый набор значений записывается в два раза, в то время как есть только одна такая величина в лог-файл, который читается. Этот тип дублирования происходит случайным образом для многих значений при обработке полного файла журнала.
Такое поведение не наблюдается ни для одной из предыдущих переменных, присутствующих в коде.