Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Python: Printing a list from a txt file Need help trying to print a list from a

ID: 3706812 • Letter: P

Question

Python: Printing a list from a txt file

Need help trying to print a list from a txt file. Unfortunately, it's not a CSV.

The file is located here: http://www.filedropper.com/movies_1 or here http://s000.tinyupload.com/index.php?file_id=48953557772434487729

Exercise:

??This option shall prompt the user for a year between 1880 and 2050 inclusive and then output all of the movie titles for that year, in the same order as they occur in the input file:

Example output:

?______________________

Admiral Cigarette

Biter Bit, The

Bowery Waltz

. . .

Niagara

President McKinley at Home

Seminary Girls

_________________________________

This is my set-up. Unfortunately, for the life of me, I can't ge the body of the syntax to work.

Explanation / Answer

Please refer the following code:

import re

file_ref = open("movies.txt", 'r')
line1 = 0;
year_input = int(input("Please enter a year from 1880-2050:"))
if year_input < 1880 and year_input > 2050:
print("Invalid year")
  
else:
for line in file_ref:
if(str(year_input) not in line):
continue
else:
my_movie=line.split(str(year_input),1)[0]
print(my_movie)
file_ref.close()

_________________________________________________________________

Explaination:

First of all we are checking if the year we entered is present in the line or not. with the help of not in.
if the line is not present then we will continue.
if the line is present, we are spliting the line with year_input as a separator.

line.split(str(year_input),1)[0]
after splitting we will get two strings one is before year_input and another is after year_input
we require the string which is before year_input that is why we have mentioned [0]