Python Forum
Print the line before the corrent line
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Print the line before the corrent line
#1
Greetings!
I'm searching lines for two words "PASSED" and "FAILED"
I need to get the line before "PASSED" and the "FAILED" line.
I'm definitely doing it wrong,
the script I wrote does not get me the line I'm looking for, it actually prints the same line.
ftor = open('C:/01/prt.csv','r')
for num, el in enumerate(ftor, 1):
    el=el.strip()
    if 'PASSED' in el :

        to_gt =num-1
        print(f" BEFORE PASSED ->{to_gt} - {el}")    

        pas_fail = el.split(",")
        print (f"    {num}    {pas_fail[9]} - {el}")
        print("")
    elif 'FAILED' in el :
        to_gt =num-1
        print(f" BEFORE FAILED ->{to_gt} - {el}")     

        pas_fail = el.split(",")
        print (f"    {num}    {pas_fail[9]} - {el}")
        print("") 

ftor.close()

Thank you!
Reply
#2
I don't understand your code, but if you just need to remember the last line for some reason:
prev_line = None
for line in file:
    do stuff(line, prev_line)
    prev_line = line 
tester_V likes this post
Reply
#3
Awesome!
Thank you, guys!
Dance
Reply
#4
Are you guys all in the same class? I still have the csv sample from some other thread about finding "Failed"

The pass/fail column is column F

How big is your ftor?

import sys

path2csv = '/home/pedro/myPython/csv/csv/YE2531_20220609_074155_Tester1.csv'

with open(path2csv) as mycsv:
    count = 1
    data = mycsv.readlines()
    print('The size of data is', sys.getsizeof(data))
    fails = []
    for line in data:
        print('The size of line is', sys.getsizeof(line))
        if 'Failed' in line:
            print('line', count, 'failed')
            print('of course, the previous line was', count-1)
            fails.append(count)
        count +=1

for f in fails:
    print('fail line number is', f)
Reply
#5
That is funny!
No, we are not in the same class for sure.
I'm a 61-year-old guy trying the get some data from machines...

It is hard to learn anything if you are that old... Wink
Reply
#6
Youngster!
tester_V and buran like this post
Reply
#7
Larz60+!
You are DA MAN
Reply
#8
It's a shame you're on Windows. Otherwise, sounds like a job for grep and head probably.
Reply
#9
I just asked if ftor is very big, maybe millions of lines, because then a function with yield may be better.

Then you need the experts here to design that, yield confuses me!
tester_V likes this post
Reply
#10
One can use itertools.pairwise()
for lineno, (prev_line, line) in enumerate(itertools.pairwise(ftor), 2):
    ... # do something with line and prev_line
tester_V likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Line graph with two superimposed lines sawtooth500 4 349 Apr-02-2024, 08:56 PM
Last Post: sawtooth500
  How to add multi-line comment section? Winfried 1 220 Mar-24-2024, 04:34 PM
Last Post: deanhystad
  break print_format lengthy line akbarza 4 385 Mar-13-2024, 08:35 AM
Last Post: akbarza
  Reading and storing a line of output from pexpect child eagerissac 1 4,274 Feb-20-2024, 05:51 AM
Last Post: ayoshittu
  coma separator is printed on a new line for some reason tester_V 4 495 Feb-02-2024, 06:06 PM
Last Post: tester_V
  problem with spliting line in print akbarza 3 399 Jan-23-2024, 04:11 PM
Last Post: deanhystad
  Unable to understand the meaning of the line of code. jahuja73 0 312 Jan-23-2024, 05:09 AM
Last Post: jahuja73
  Receive Input on Same Line? johnywhy 8 731 Jan-16-2024, 03:45 AM
Last Post: johnywhy
  Reading in of line not working? garynewport 2 857 Sep-19-2023, 02:22 PM
Last Post: snippsat
  'answers 2' is not defined on line 27 0814uu 4 743 Sep-02-2023, 11:02 PM
Last Post: 0814uu

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020