Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: How to print one more line after my result from file?

Tags:

In my input file I have many lines, I am searching only one, which meets my requirements. And that's already done. But I need to print a line after this line which has been already found.

Example of input:

line 1 x
line 2 a
line 3 a
line 3 a

I am searching for line where is x inside.

for lines in input:
  if 'x' in lines:
    print (lines)

Result: line 1 x
So now I need to show one line after my result

Expected result:

line 1 x
line 2 a

I also tried:

for lines in input:
  if 'x' in lines:
    print (lines, '\n', lines[lines.index(lines) + 0:100])
like image 815
sygneto Avatar asked May 12 '18 10:05

sygneto


1 Answers

Try splitting your input into a list first:

a = input.split('\n')

for lines in a:
    if 'x' in lines:
        print (lines, '\n', a[a.index(lines) + 1])
like image 196
WholesomeGhost Avatar answered Sep 28 '22 18:09

WholesomeGhost