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!
buran and tester_V 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
  No new line from print in a browser Lou 13 1,076 Dec-01-2025, 06:39 PM
Last Post: noisefloor
  cannot unpack non-iterable int object in urllib3/util/wait.py", line 85, ping_chen_ibm_us 2 886 Aug-01-2025, 02:05 PM
Last Post: ping_chen_ibm_us
  [SOLVED] Why does regex fail cleaning line? Winfried 7 4,849 Jul-11-2025, 11:52 PM
Last Post: Pedroski55
  I am a newbie I like to use the command line Mikel2025 1 1,018 Jun-13-2025, 03:20 PM
Last Post: Gribouillis
  Βad Input on line 12 Azdaghost 5 2,159 Apr-19-2025, 10:22 PM
Last Post: Azdaghost
Question [SOLVED] [Beautiful Soup] Move line to top in HTML head? Winfried 0 945 Apr-13-2025, 05:50 AM
Last Post: Winfried
  Insert command line in script lif 4 2,169 Mar-24-2025, 10:30 PM
Last Post: lif
  Entry field random pull from list, each return own line Bear1981 6 1,838 Feb-25-2025, 06:09 AM
Last Post: Pedroski55
  How to revert back to a previous line from user input Sharkenn64u 2 2,989 Dec-28-2024, 08:02 AM
Last Post: Pedroski55
  Pandas - error when running Pycharm, but works on cmd line zxcv101 2 3,502 Sep-09-2024, 08:03 AM
Last Post: pinkang

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.