1

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 ?

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
Ziathes
  • 15
  • 5

3 Answers3

2

Maybe the easiest way to do what you want is to use loops sequentially

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(textFile)
        for line in it:
            if '/MAT/LAW02/1' in line:  # once it's found, you exit the loop
                break
        for i, line in enumerate(it):
            if i > 12:
                break
            # here do your copy stuff

#    textFile.close()
# textFileClean.close()
# No need to close the file, it's done automatically when you exit the `with`statement

NathanV
  • 31
  • 3
1

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.

DRPK
  • 2,023
  • 1
  • 14
  • 27
trincot
  • 317,000
  • 35
  • 244
  • 286
1

For similar questions just like your case, one of the main and famous algorithms is Feature toggle method; just set variable for checking when we should write in the file and when we should not, try this:

Read more on: https://en.wikipedia.org/wiki/Feature_toggle

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


header = '/MAT/LAW02/1'
catch_lines_toggle = False
lines_counter = 0
max_lines = 20
with open(inputRadFile, "r") as textFile:
    for each_line in textFile:
        if header in each_line:
            catch_lines_toggle = True

        if catch_lines_toggle is True:
            with open(outputRadFile, "a") as textFileClean:
                catchInfo = each_line.strip().split()
                toString = ''.join(catchInfo)
                textFileClean.write(toString+"\n")

            lines_counter += 1
            if lines_counter == max_lines:
                catch_lines_toggle = False
                break

Pythonic way (without memory efficiency - good for small files):

with open("Configs.py", "r") as textFile:
    data = textFile.read()

target_lines = ["".join(each_line.strip().split()) for each_line in data.split('/MAT/LAW02/1')[1].split("\n")[0:13]]

with open("demo/textB.txt", "w") as output:
    output.write("\n".join(target_lines))
DRPK
  • 2,023
  • 1
  • 14
  • 27