Question

In: Computer Science

Goal: to write a Python program that will read a playlist from a CSV file and...

Goal: to write a Python program that will read a playlist from a CSV file and display it in the console in the form of a table.

https://s3.eu-west-2.amazonaws.com/bb-python-modules/Coursework/CW3/playlist_text_question.html

The following is a link to the question. It includes all instruction and a template file (with additional instructions) where the answer must be completed.

Solutions

Expert Solution

Given below is the code for the question. PLEASE MAKE SURE INDENTATION IS EXACTLY AS SHOWN IN IMAGE.
Please do rate the answer if it helped. Thank you.

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


## Import module for handling csv files.
import csv
## You are not allowed to import any other module into this file.

## display_playlist( filename )
## Replace the following dummy function with one that reads the playlist
## from the csv file corresponding to the filename argument and prints
## out the playlist in the format shown in the coursework specification
## document.
##

## Note: Check whether the filename argument ends in ".csv". If it doesn't
## then you should add ".csv" to get the actual name of the CSV file that
## will be opened.

def display_playlist( filename ):
   if not filename.endswith('.csv'):
       filename = filename + ".csv"
  
   print( '\n Running display_playlist( "{}" )'.format(filename) )
   data = get_datalist_from_csv(filename)
   line = '-' * 113;
   print(line)
   print('|', pad_to_length(data[0][0], 40), '|', pad_to_length(data[0][1], 25), '|', pad_to_length(data[0][2], 30), '|', pad_to_length(data[0][3], 5), '|')
   print(line)
   for i in range(1, len(data)):
       print('|', pad_to_length(data[i][0], 40), '|', pad_to_length(data[i][1], 25), '|', pad_to_length(data[i][2], 30), '|', pad_to_length(data[i][3], 5), '|')
   print(line)
  
## You can use the following function to read data from a csv format file
def get_datalist_from_csv( filename ):
   ## Create a 'file object' f, for accessing the file:
   with open( filename ) as f:
       reader = csv.reader(f) # create a 'csv reader' from the file object
       datalist = list( reader ) # create a list from the reader
   return datalist


## The following function may be useful for aligning your table columns
## It adds spaces to the end of a string to make it up to length n.
def pad_to_length( string, n):
   return string + " "* (n-len(string)) ## s*n gives empty string for n<1

## Run the display_playlist function on the example files
## (The ".csv" should get added by display_playlist)
display_playlist( "geek-music.csv" )
display_playlist( "snake-music")



Related Solutions

build a python program that will be performing: - Read a CSV file 'annual.csv' enterprise into...
build a python program that will be performing: - Read a CSV file 'annual.csv' enterprise into a data structure - Count the number of rows and columns - Determine if the data contains empty values - Replace the empty values by 'NA' for strings, '0' for decimals and '0.0' for floats - Transform all Upper case characters to Lower case characters - Transform all Lower case characters to Upper case characters - save back the 'repaired' array as csv -...
Write a program in python to read from a file the names and grades of a...
Write a program in python to read from a file the names and grades of a class of students to calculate the class average, the maximum, and the minimum grades. The program should then write the names and grades on a new file identifying the students who passed and the students who failed. The program should consist of the following functions: a) Develop a getGrades() function that reads data from a file and stores it and returns it as a...
Python DESCRIPTION Write a program that will read an array of integers from a file and...
Python DESCRIPTION Write a program that will read an array of integers from a file and do the following: ● Task 1: Revert the array in N/2 complexity time (i.e., number of steps) . ● Task 2: Find the maximum and minimum element of the array. INPUT OUTPUT Read the array of integers from a file named “ inputHW1.txt ”. To do this, you can use code snippet from the “ file.py ” file. This file is provided in Canvas....
Python DESCRIPTION Write a program that will read an array of integers from a file and...
Python DESCRIPTION Write a program that will read an array of integers from a file and do the following: ● Task 1: Revert the array in N/2 complexity time (i.e., number of steps) . ● Task 2: Find the maximum and minimum element of the array. INPUT OUTPUT Read the array of integers from a file named “ inputHW1.txt ”. To do this, you can use code snippet from the “ file.py ” file. This file is provided in Canvas....
Python DESCRIPTION Write a program that will read an array of integers from a file and...
Python DESCRIPTION Write a program that will read an array of integers from a file and do the following: ● Task 1: Revert the array in N/2 complexity time (i.e., number of steps) . ● Task 2: Find the maximum and minimum element of the array. INPUT OUTPUT Read the array of integers from a file named “ inputHW1.txt ”. To do this, you can use code snippet from the “ file.py ” file. This file is provided in Canvas....
Write a python program to read from a file the names and grades of a class...
Write a python program to read from a file the names and grades of a class of students to calculate the class average, the maximum, and the minimum grades. The program should then write the names and grades on a new file identifying the students who passed and the students who failed. The program should consist of the following functions: a) Develop a gradesInput() function that reads data from a file and stores it and returns it as a dictionary....
Write a Java program to read in the 10 numbers in the example file Book1.csv provided...
Write a Java program to read in the 10 numbers in the example file Book1.csv provided above. The program should sum all the numbers, find the lowest number, find the highest number, and computer the average. Upon completion of the processing, the program should write a new text file named stats.txt with the information found in the following format where xxx represents a number calculated above. The sum of the numbers is: xxx The lowest number is: xxx The highest...
Using Python read dataset in the HTML in beautiful way. You need to read CSV file...
Using Python read dataset in the HTML in beautiful way. You need to read CSV file ( Use any for example, You can use small dataset) You need to use pandas library You need to use Flask Make search table like YouTube has.
Problem 9 - PYTHON There is a CSV-formatted file called olympics2.csv. Write code that creates a...
Problem 9 - PYTHON There is a CSV-formatted file called olympics2.csv. Write code that creates a dictionary named country_olympians where the keys are country names and the values are lists of unique olympians from that country (no olympian's name should appear more than once for a given country). Name,Sex,Age,Team,Event,Medal A Dijiang,M,24,China,Basketball,NA A Lamusi,M,23,China,Judo,NA Gunnar Nielsen Aaby,M,24,Denmark,Football,NA Edgar Lindenau Aabye,M,34,Sweden,Tug-Of-War,Gold Christine Jacoba Aaftink,F,21,Netherlands,Speed Skating,NA Christine Jacoba Aaftink,F,21,Netherlands,Speed Skating,NA Christine Jacoba Aaftink,F,25,Netherlands,Speed Skating,NA Christine Jacoba Aaftink,F,25,Netherlands,Speed Skating,NA Christine Jacoba Aaftink,F,27,Netherlands,Speed Skating,NA Christine...
Step by step in python please Write a program this will read a file (prompt for...
Step by step in python please Write a program this will read a file (prompt for name) containing a series of numbers (one number per line), where each number represents the radii of different circles. Have your program output a file (prompt for name) containing a table listing: the number of the circle (the order in the file) the radius of the circle the circumference the area of the circle the diameter of the circle Use different functions to calculate...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT