import string searchstring = "1990" limit = 5 def main(): # Reads file top250.txt and prints the lop limit movies in the top 250 ranking # for the year specified in searchstring infile = open("top250.txt") infile.readline() infile.readline() infile.readline() total = 0 print "Top", limit, "movies of", searchstring while total < limit: # read the next line in file and split in spaces line = infile.readline() if line == "": total = limit fields = line.split(" ") # extract the year of the movie lastField = fields[len(fields)-1] year = lastField[1:5] if year == searchstring: total = total + 1 # join converts a list into a string print " ".join(fields) infile.close() main()