Search code examples
pythonpython-3.xiteratorenumerate

How to continue enumarete() loop in other loop


I'm stuck with this:

I want to grab the future 12 lines from my files when it finds '/MAT/LAW02/1' to write it, in a second file.

And after that, I want it to keep analyzing until the end.

But I'm stuck because I can never find a topic on this problem.

Here is my current code:

inputRadFile = "demo/textA.txt"
outputRadFile = "demo/textB.txt"

with open(outputRadFile, "w") as textFileClean:
    with open(inputRadFile, "r") as textFile:
        for i, line in enumerate(textFile):
            if '/MAT/LAW02/1' in line:
                catchInfo = line.strip().split()
                toString = ''.join(catchInfo)
                textFileClean.write(toString)
    textFile.close()
textFileClean.close()

This is a snippet of textA file that I want to extract (because the textA file has 200,000 lines) :

/MAT/LAW02/1
ES_ODG2_MED-5                                                                                      
#                RHO|           REF. RHO|
             7.82E-9
#                  E|                  v|
             210000.                 0.3
#                  a|                  b|                  n|               emax|               smax|
               273.1               437.6               0.724               1.E30               1.E30
#                  c|                 e0|      ICC|  Fsmooth|               Fcut|              Chard|
               0.097                0.32         1         0               1.E30
#                  m|              Tmelt|             rho0Cp|                 Ti|
                  0.                  0.                  0.                298.

And here is my textB file after running the above code:

/MAT/LAW02/1

And I thought of something like this:

from itertools import islice

inputRadFile = "demo/textA.txt"
outputRadFile = "demo/textB.txt"

with open(outputRadFile, "w") as textFileClean:
    with open(inputRadFile, "r") as textFile:
        it = iter(enumerate(textFile))
        for i, line in it:
            x = 0
            y = 12
            if '/MAT/LAW02/1' in line:
                while x != y:
                    catchInfo = line.strip().split()
                    toString = ''.join(catchInfo)
                    textFileClean.write(toString)
                    place_where_skip_happened = i
                    next(islice(it, 1, 1), None)
                    x += 1
    textFile.close()
textFileClean.close()

I wanted to go from 1 by 1 to 12.

I was inspired by this topic : Skip iterations in enumerated list object (python)

But it does not work for me.

Here is my textB file after running this code:

/MAT/LAW02/1/MAT/LAW02/1/MAT/LAW02/1/MAT/LAW02/1/MAT/LAW02/1/MAT/LAW02/1/MAT/LAW02/1/MAT/LAW02/1/MAT/LAW02/1/MAT/LAW02/1/MAT/LAW02/1/MAT/LAW02/1

The separator is not a problem at the moment (I know how to do it).

At the end I want a textB like the snippet of textA.

Someone can help me ?


Solution

  • Maybe keep the original loop, but add a counter for the 12 lines, and allow the if block to execute if either the line has a match or the counter is non zero:

    with open(outputRadFile, "w") as textFileClean:
        with open(inputRadFile, "r") as textFile:
            counter = 0
            for i, line in enumerate(textFile):
                if '/MAT/LAW02/1' in line:
                    counter = 13  # the line itself plus the 12 that follow 
                if counter > 0:
                    counter -= 1 
                    catchInfo = line.strip().split()
                    toString = ''.join(catchInfo)
                    textFileClean.write(toString)
    

    This will also work better when two matches occur within a few lines (less than 12). This will extend the output to 12 lines after the second of those matches.