Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Print the next 3 lines
#1
Once i enter in this loop, how could i print not only the current line, but the next 3 lines after the current line ?

for line in work01:
    if any(word in line for word in inc_list):
        print(line)
Reply
#2
If you don't want overlapping occurrences of the pattern, you could do
from itertools import islice
items = iter(work01)  # <- we need an iterator, not just an iterable
for line in items:
    if any(word in line for word in inc_list):
        print(line)
        for subsequent in islice(items, None, 3):
            print(subsequent)
« We can solve any problem by introducing an extra level of indirection »
Reply
#3
Maybe get a slice of the list if: any(word in line for word in inc_list) = True?

from random import choice

words = ['ant', 'bee', 'cauliflower', 'cow', 'bean']
work01_list = [f'Line {i} stuff line {choice(words)}.\n' for i in range(1,11)]
inc_list = ['bean', 'cauliflower']

for i in range(len(work01_list)):
    line = work01_list[i]
    if any(word in line for word in inc_list):
        list_slice = work01_list[i:i+4]
        for w in inc_list:
            if w in list_slice[0]:
                word = w
        for j in list_slice:
            print(f'word = {word}, {j}')
        print('End of slice ... \n')
        # put break here to stop after the first 4 lines
        #break
Gribouillis said something about not overlapping. You could do that like this:

work01_gen = (f'Line {i} stuff line {choice(words)}.\n' for i in range(1,31))
for w in work01_gen:
    try:
        if any(word in w for word in inc_list):
            print('Found a wanted word, now printing the next 3 lines ... ')
            for i in range(3):
                print(next(work01_gen))
    except StopIteration:
        print('Run out of generator items!')
Reply
#4
You can use a counter to keep track of the lines. Modify your loop to print the current line and then the next three lines by using the
islice
function from the
itertools
module. Here is an example:


[inline]from itertools import islice

for i, line in enumerate(work01):
if any(word in line for word in inc_list):
print(line)
for next_line in islice(work01, i+1, i+4):
print(next_line)
[/inline]

This will print the matched line and the following three lines.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  failing to print not matched lines from second file tester_V 14 6,497 Apr-05-2022, 11:56 AM
Last Post: codinglearner
  why print('\n') produced 2 new lines instead of 1 - Located inside a FOR loop JulyFire 2 2,688 Jan-10-2021, 01:50 AM
Last Post: JulyFire
  Need print out lines before and after the match tester_V 6 7,219 Aug-11-2020, 02:50 AM
Last Post: deanhystad
  Iterate 2 large text files across lines and replace lines in second file medatib531 13 6,355 Aug-10-2020, 11:01 PM
Last Post: medatib531
  print python json dump onto multiple lines lhailey 2 20,356 Mar-02-2020, 12:47 PM
Last Post: vishalhule
  Read serial BR=9600 . print me 2 lines? korenron 1 2,359 Dec-31-2019, 06:39 PM
Last Post: DeaD_EyE
  Print multiple record lines ntigner 3 3,552 Feb-08-2018, 05:34 PM
Last Post: nilamo
  Only one of two lines print/syntax error naysjp 2 3,860 Jan-12-2017, 07:08 AM
Last Post: wavic
  Print list items on separate lines with additional text DBS 2 6,289 Jan-11-2017, 02:57 AM
Last Post: DBS

Forum Jump:

User Panel Messages

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