> Searching for specific things in a file using Python?

Searching for specific things in a file using Python?

Posted at: 2014-12-18 
Instead of searching, would you already know how to display all the records in the file?

You seem to be implying that there is a serial number of every 4th line.

Would you know how to just display every 4th line?

Can you show us your code to do that?

=============

Edit: Python2

Change the file name to your file and see if this prints out only the lines with serial number.

file = open ("phelp.txt")

data = file.readlines ()

i = 0

for line in data:

--- if (i%4==0): print line

--- i += 1

file.close ()

Okay, so I'm doing a practise controlled assessment for computer science, and I'm really struggling with this particular part as I really only started learning Python just 4 weeks ago.

The task asks you to search for a serial number within a text file and then print out the relevant details to accompany it, which can be found in the 3 lines underneath the serial number. I honestly am not sure how to approach this and I've only got as far as

opening the file in read mode on Python :) Any help would be much appreciated.