M. Koz
M. Koz

Reputation: 41

Trying to skip over several lines but the skipped lines are still being worked on

My program takes an input file, reads the file using whitespace as the delimiter and puts the data into an array, then I want to iterate over each line and if certain strings are found write that info to another file.

When a specific string is found, I want to skip over several lines, meaning that these lines are NOT iterated over. I thought that if I increased the 'line' variable (i) that would do it, but despite the fact that i is increased by 50, those 50 lines are still being worked on, which is not what I want.

Hopefully I have explained this problem well. Thank you in advance for your feedback.

def create_outfile(infile):

    gto_found = 0
    outfile = "output.txt" # Output file
    outfile = open(outfile,'w') # Open output file for writing
    for i in range(len(infile)): # iterate over each line
        if len(infile[i]) == 6: 
            if (infile[i][4][1:-1]) == "GTO" and gto_found == 0: # now skip
                print (i)
                print (infile[i])
                debugPause = input("\nPausing to debug...\n")
                i = i + 50 # Skip over the GTO section
                gto_found = 1
                print (i)
                debugPause = input("\nPausing to debug...\n")
                print (infile[i])

        for j in range(len(infile[i])): # iterate over each element

            # Command section
            if (infile[i][j])[:5] == "@ACS_": 
                # Do some work

Upvotes: 1

Views: 104

Answers (1)

Jo Bay
Jo Bay

Reputation: 106

Unfortunately, python does not allow a for loop to jump up like that. The variable i cannot be edited inside the loop. This is same as this question here, so check it out. This other topic shows some work around that you could use.

Upvotes: 1

Related Questions