How can I get a line in a file for a key word in the next line? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How can I get a line in a file for a key word in the next line?

for example: if "bike" is in the line, print the previous line

15th Jan 2017, 9:14 AM
MIGUEL GARCÍA GÓMEZ
MIGUEL GARCÍA GÓMEZ - avatar
1 Answer
+ 2
u can use this lines = [line.rstrip('\n') for line in open("filename.txt")] to put the lines into a list then u can access each line with the list index - print(lines[0]) will print the first line of the text and same with the other lines just change the index number
15th Jan 2017, 9:25 AM
Kawaii
Kawaii - avatar