In: Computer Science
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.
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")