Need to find two lines, Line1 and the last of a Line2 and print lines in pairs.
Need to find two lines, Line1 and the last of a Line2 and print lines in pairs.
(OP)
Trying to pull out paired lines, LINE 1 and LINE 2 from a file for farther processing.
The file goes like this:
Some lines
LINE 1
Some lines
Some lines
LINE 2
LINE 2
LINE 2
Some lines
Some lines
Some lines
LINE 1
Some lines
LINE 2
And so on.
I’d like to print:
LINE 1
LINE 2 (preferably last of LINE 2)
Also, if after the last LINE 1 no LINE 2 found I’d like to print the last line of the file.
The Python3.x code I’m come up so far prints All LINE 1 and LINE 2 not what I want.
The file goes like this:
Some lines
LINE 1
Some lines
Some lines
LINE 2
LINE 2
LINE 2
Some lines
Some lines
Some lines
LINE 1
Some lines
LINE 2
And so on.
I’d like to print:
LINE 1
LINE 2 (preferably last of LINE 2)
Also, if after the last LINE 1 no LINE 2 found I’d like to print the last line of the file.
The Python3.x code I’m come up so far prints All LINE 1 and LINE 2 not what I want.
CODE --> python
filepath = 'mytext1.txt' line1 = 'LINE 1' line2 = 'LINE 2' with open(filepath) as fp: line = fp.readline() while line: if line1 in line: print("string found in line ---"+line) if line2 in line: print("string found in line ---"+line) line = fp.readline()
RE: Need to find two lines, Line1 and the last of a Line2 and print lines in pairs.
Make the contents of the LINE2 buffer empty.
When you find a LINE1, if the flag is set, print the LINE2 buffer
Set it to False
Set it to True when you find LINE2 - just save LINE2 but don't print it
When you exit the loop, if the flag is set, print LINE2.
RE: Need to find two lines, Line1 and the last of a Line2 and print lines in pairs.
I need to print the last of Line 2 for each of Line 1.
Some lines
LINE 1----------> print this one (pair one)
Some lines
Some lines
LINE 2
LINE 2
LINE 2----------> print this one (pair one)
Some lines
Some lines
Some lines
LINE 1----------> print this one (pair two)
Some lines
LINE 2----------> print this one (pair two)
And so on.
RE: Need to find two lines, Line1 and the last of a Line2 and print lines in pairs.
RE: Need to find two lines, Line1 and the last of a Line2 and print lines in pairs.
Probably helpful to keep track of what lines you found things on.
But are you sure that you understand your assignment?
RE: Need to find two lines, Line1 and the last of a Line2 and print lines in pairs.
CODE
Output on the data you posted:
CODE
RE: Need to find two lines, Line1 and the last of a Line2 and print lines in pairs.
but I do not understand how to "translate it" to code.
I really do not understand how it all works for some reason.
And the whole code structure without some kind of braces is really confusing for me.
It is beyond me....
here the snippet with the "flag" but now it prints only LINE 1. It is clear the second nested loop is not working and I do not understand why.
CODE --> python
RE: Need to find two lines, Line1 and the last of a Line2 and print lines in pairs.
I do not understand how did you get the second or third of the LINE2:
LINE 1----------> print this one (pair one)
Some lines
Some lines
LINE 2
LINE 2
LINE 2----------> print this one (pair one)
I understand it is all happening here but I do not understand how
CODE --> Python
RE: Need to find two lines, Line1 and the last of a Line2 and print lines in pairs.
Before the loop I create two variables which holds the lines from pair and initialize them:
line_pair1 = ""
line_pair2 = ""
Then I read lines in the loop as you programmed before.
Every time when the line2 occurs i store it in the variable line_pair2.
But if the line1 occurs, before reading it in line_pair1, i first try to print the previous pair - so i ask: is the variable line_pair2 not empty ? Because if the variable line_pair2 is not empty, that means that I found the pair in the loop before. So I print the previous pair. Then for searching the next pair, i initialize line_pair2 and store the beginning line of the next pair into line_pair1. At the end of file i print the last pair found if available.
To understand better how it works consider this data file:
CODE
on the beginning when this line comes
CODE
In this case we only store this line into variable line_pair1, i.e.
we have:
line_pair1 = "LINE 1----------> print this one (pair one)"
When lines
CODE
When line
CODE
line_pair2 = "LINE 2"
When line
CODE
line_pair2 = "LINE 2----------> print this one (pair one)"
on lines
CODE
Now when this line comes (i.e. the beginning of the next pair)
CODE
line_pair1 = "LINE 1----------> print this one (pair one)"
line_pair2 = "LINE 2----------> print this one (pair one)"
So we print the previous pair, then initialize the variable line_pair2 and store
the current line into variable line_pair1.
Now we have in the pair variables
line_pair1 = "LINE 1----------> print this one (pair two)"
line_pair2 = ""
and by reading the next lines we are collecting the next pair.
on
CODE
on
CODE
At end of file we try to print the last pair if available.
In this case it exists, because the variable line_pair2 is not empty.
We have in our pair variables:
line_pair1 = "LINE 1----------> print this one (pair two)"
line_pair2 = "LINE 2----------> print this one (pair two)"
So we print the last pair and end.
RE: Need to find two lines, Line1 and the last of a Line2 and print lines in pairs.
line_pair2 = "" means the same as pair_found = False and
line_pair2 != "" means the same as pair_found = True
RE: Need to find two lines, Line1 and the last of a Line2 and print lines in pairs.
How var "line_pair2" can have anything if nothing is passed to it or assign to it?
CODE --> python
RE: Need to find two lines, Line1 and the last of a Line2 and print lines in pairs.
you can see, that the variable value will be assigned in the while-loop in this if-block:
CODE
RE: Need to find two lines, Line1 and the last of a Line2 and print lines in pairs.
Thank you!