×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

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.

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.

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.

Create a flag to indicate whether LINE2 has been found. Set it to False initially.
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.

(OP)
I'm confused, ( I do not know what the "flag is" but I'm already printing Lines 1 and Lines 2.
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.

Flag = boolean variable. Basically save the LINE2 and only print it when you get a LINE1.

RE: Need to find two lines, Line1 and the last of a Line2 and print lines in pairs.

Then you will need to loop through the file multiple times.

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.

Try something like this, comments how it works are in the code:

CODE

filepath = 'mytext1.txt'
line1 = 'LINE 1'
line2 = 'LINE 2'


line_pair1 =  ""
line_pair2 =  ""
with open(filepath) as fp:  
    line = fp.readline() 

    while line:
        # remove \n from line
        line = line[:-1]
        #print("processing line: " + line)
        if line1 in line:
           if line_pair2 != "":
              print("* line pair found:")
              print(line_pair1) 
              print(line_pair2) 
              # initialize second line from pair
              line_pair2 = ""
           # set first line of pair
           line_pair1 = line

        if line2 in line:
           line_pair2 = line

        # next line
        line = fp.readline()            
     
    # finally at end of file, print the last pair
    if line_pair1 != "" and line_pair2 != "":
       print("* line pair found:")
       print(line_pair1) 
       print(line_pair2) 

Output on the data you posted:

CODE

$ python3 tester_v2.py
* line pair found:
LINE 1----------> print this one (pair one)
LINE 2----------> print this one (pair one)
* line pair found:
LINE 1----------> print this one (pair two)
LINE 2----------> print this one (pair two) 

RE: Need to find two lines, Line1 and the last of a Line2 and print lines in pairs.

(OP)
Thanks to "xwb" and "mintjulep"! I really appreciate your help. I understand your logic
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

filepath = 'mytext1.txt'
line1  = 'LINE 1'
line2 = 'LINE 2'

flag =False


with open(filepath) as fp:  
    line = fp.readline() 

    while line:
        if line1 in line:
            print("string found in line ---"+line)
            flag=True
            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.

(OP)
mikrom, this is amazing code man!
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

if line1 in line:
           if line_pair2 != "":
              print("* line pair found:")
              print(line_pair1) 
              print(line_pair2) 
              # initialize second line from pair
              line_pair2 = ""
           # set first line of pair
           line_pair1 = line

        if line2 in line:
           line_pair2 = line 

RE: Need to find two lines, Line1 and the last of a Line2 and print lines in pairs.

Hi Tester_V,
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

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. 


on the beginning when this line comes

CODE

LINE 1----------> print this one (pair one) 
we have line_pair2 = "", so the pair will not be printed.
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

Some lines 
come we read next line

When line

CODE

LINE 2 
first and second time occurs we store it in line_pair2, so we have
line_pair2 = "LINE 2"
When line

CODE

LINE 2----------> print this one (pair one) 
third time occurs we store it in line_pair2, so now we have
line_pair2 = "LINE 2----------> print this one (pair one)"

on lines

CODE

Some lines 
we read next line

Now when this line comes (i.e. the beginning of the next pair)

CODE

LINE 1----------> print this one (pair two) 
we have in the pair variables the previous pair:
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

Some lines 
we read next line
on

CODE

LINE 2----------> print this one (pair two) 
we store the current line into line_pair2

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.

In principle, the variable line_pair2 serves as a boolean flag pair_found.
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.

(OP)
more questions.
How var "line_pair2" can have anything if nothing is passed to it or assign to it?

CODE --> python

if line1 in line:
           if line_pair2 != "":  <<------------ nothing is asign to the to it 

RE: Need to find two lines, Line1 and the last of a Line2 and print lines in pairs.

if you look at the code I posted above on 1 May 20 09:30,
you can see, that the variable value will be assigned in the while-loop in this if-block:

CODE

...
    while line:
        ...
        ...
        if line2 in line:
           line_pair2 = line

        # next line
        line = fp.readline()
... 

RE: Need to find two lines, Line1 and the last of a Line2 and print lines in pairs.

(OP)
Amazing!
Thank you!

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close