Knowee
Questions
Features
Study Tools

5.15 LAB: Movie show time displayWrite a program that reads movie data from a CSV (comma separated values) file and output the data in a formatted table. The program first reads the name of the CSV file from the user. The program then reads the CSV file and outputs the contents according to the following requirements:Each row contains the title, rating, and all showtimes of a unique movie.A space is placed before and after each vertical separator ('|') in each row.Column 1 displays the movie titles and is left justified with a minimum of 44 characters.If the movie title has more than 44 characters, output the first 44 characters only.Column 2 displays the movie ratings and is right justified with a minimum of 5 characters.Column 3 displays all the showtimes of the same movie, separated by a space.Each row of the CSV file contains the showtime, title, and rating of a movie. Assume data of the same movie are grouped in consecutive rows.Hints: Use the find() function to find the index of a comma in each row of the text file. Use the substr() function to extract texts separated by the commas.Ex: If the input of the program is:

Question

5.15 LAB: Movie show time displayWrite a program that reads movie data from a CSV (comma separated values) file and output the data in a formatted table. The program first reads the name of the CSV file from the user. The program then reads the CSV file and outputs the contents according to the following requirements:Each row contains the title, rating, and all showtimes of a unique movie.A space is placed before and after each vertical separator ('|') in each row.Column 1 displays the movie titles and is left justified with a minimum of 44 characters.If the movie title has more than 44 characters, output the first 44 characters only.Column 2 displays the movie ratings and is right justified with a minimum of 5 characters.Column 3 displays all the showtimes of the same movie, separated by a space.Each row of the CSV file contains the showtime, title, and rating of a movie. Assume data of the same movie are grouped in consecutive rows.Hints: Use the find() function to find the index of a comma in each row of the text file. Use the substr() function to extract texts separated by the commas.Ex: If the input of the program is:

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

Here is a step-by-step guide on how to write this program in Python:

  1. Import the csv module: This module implements classes to read and write tabular data in CSV format.
import csv
  1. Prompt the user to enter the name of the CSV file.
filename = input("Enter the name of the CSV file: ")
  1. Open the CSV file and create a csv.reader object.
with open(filename, 'r') as file:
    reader = csv.reader(file)
  1. Iterate over each row in the CSV file. Each row is a list of strings where each string is a field.
    for row in reader:
  1. Extract the movie title, rating, and showtimes from each row. Since the CSV file contains the showtime, title, and rating of a movie in each row, you can use indexing to access these fields.
        showtime = row[0]
        title = row[1][:44]  # get the first 44 characters of the title
        rating = row[2]
  1. Format and print the movie data according to the requirements. You can use the format() function to format the strings.
        print("| {:<44} | {:>5} | {:} |".format(title, rating, showtime))

Here is the complete program:

import csv

filename = input("Enter the name of the CSV file: ")

with open(filename, 'r') as file:
    reader = csv.reader(file)
    for row in reader:
        showtime = row[0]
        title = row[1][:44]  # get the first 44 characters of the title
        rating = row[2]
        print("| {:<44} | {:>5} | {:} |".format(title, rating, showtime))

This program reads a CSV file and outputs the movie data in a formatted table. The movie title is left justified with a minimum of 44 characters, the movie rating is right justified with a minimum of 5 characters, and the showtimes are separated by a space.

This problem has been solved

Similar Questions

Fill in the blanks with the correct answer :Text fileComma-separated values (CSV) fileTab-delimited file(Just select the word in BOLD)blank1 - Word AnswerWrite your response here... :  A data file that contains characters, such as letters, numbers, and symbols, including punctuation and spaces.This file is not a Movie, it is not a Word, nor PPTX, etc.blank2 - Word AnswerWrite your response here... :  A file type that uses commas to separate data into columns and a newline character to separate data into rows.blank3 - Word AnswerWrite your response here... : A file type that uses tabs to separate data into columns.

Saran and Dinesh are preparing the release details for their new show 'Galactic Adventures' on their streaming service. To ensure the episode information is accurate, Saran asks Dinesh to create a program to input the episode number and its duration, and then display this information.Your task as a programmer is to assist them in this program.Input format :The input consists of an integer N, representing the episode number and a double value X, representing its duration.Output format :The output prints "Episode [N] is [X] hours long!"Refer to the sample output for the exact text and format.Code constraints :1 ≤ N ≤ 100.1 ≤ X ≤ 60.0Sample test cases :Input 1 :1 2.75Output 1 :Episode 1 is 2.75 hours long!Input 2 :2 3.75Output 2 :Episode 2 is 3.75 hours long!

Not Boring MoviesX city opened a new cinema, many people would like to go to this cinema. Thecinema also gives out a poster indicating the movies’ ratings and descriptions.Please write a SQL query to output movies with an odd numbered ID and adescription that is not 'boring'. Order the result by rating.For example, table cinema:idmoviedescriptionrating1Wargreat 3D8.92Sciencefiction8.53Irishboring6.24Ice SongFantasy8.65House cardInteresting9.1For the example above, the output should be:idmoviedescriptionrating5House cardInteresting9.11Wargreat 3D8.9Optionsselect movie, description, ratingfrom cinemawhere id % 2 != 1 and description <> 'boring'order by rating asc;select id, movie, descriptionfrom moviewhere rating % 2 = 1 and description <> 'boring'order by rating;select id, movie, description, ratingfrom cinemawhere id % 2 = 1 and description <> 'boring'order by rating desc;select id, movie, description, ratingfrom ratingwhere id % 2 = 1 and description <> 'boring'order by cinema desc;

Insert a new field called ‘Movie’ to display the results of concatenating the ‘Title’ and‘Director’ fields. Add a ‘: Directed by ’ text string in the middle of concatenate formulaas well

Aishu wants to develop a program to calculate the total viewing time for a movie marathon of the Deadpool series based on the number of movies and the frequency of breaks.She is provided with the following information:Each movie in the series has a fixed duration of 45 minutes.After watching a certain number of movies, there is a break of 15 minutes.Assist Aishu in creating a program that accepts input for the total number of movies in the series and the number of movies watched before a break. Calculate and output the total viewing time required to watch the entire series, including the breaks. Use the conditional operator for the same.Input format :The first line of input consists of an integer n, representing the total number of movies in the series.The second line consists of an integer k, representing the number of movies watched before a break.Output format :The output displays an integer representing the total viewing time required to watch the entire series, including the breaks, in minutes.Refer to the sample output for formatting specifications.Code constraints :In this scenario, the test cases fall under the following constraints:1 ≤ n ≤ 201 ≤ k ≤ 10Sample test cases :Input 1 :52Output 1 :255 minutesInput 2 :43Output 2 :195 minutesInput 3 :135Output 3 :615 minutes

1/1

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.