Python - Compose a program script that accepts input for album data for 3 albums including Artist, Title, and exactly five track titles. Collect these into a dictionary of dictionaries structure, stores the result in a JSON file and then reads the records and displays them in tabular form with column headings.
|
Input |
Output |
|
Artist – The Beatles Number of Tracks - 0 |
Artist: The Beatles Title: Abbey Road |
|
Artist – The Beatles Title – Revolver Number of Tracks – 5 Track 1 – Taxman Track 2 - Eleanor Rigby Track 3 - I’m Only Sleeping Track 4 - Love You To Track 5 – Yellow Submarine |
Artist: The Beatles Title: Revolver Taxman Eleanor Rigby I’m Only Sleeping Love You To Yellow Submarine |
CODE:
def toJSON():
# count to keep track of number of objects
count = 0
# child object
d = {}
# take user input artist
artist = input("Artist - ")
# take user input title
title = input("Title - ")
# take user input number of tracks
num = int(input("Number of Tracks - "))
# insert in dictionary
d['Artist'] = artist
d['Title'] = title
d['Number of Tracks'] = num
# stores tracks
t = []
for i in range(0, num):
track = input("Track " + str(i+1)+" - ")
# append track in array
t.append(track)
# store it in the dictionary
d['Tracks'] = t
# now store the whole object in result dictionary
# print(d)
# return the result dictionary
return d
def JSONtoTable(obj):
# printing its attributes
print("Artist: ", obj['Artist'],end=" ")
print("Title: ", obj['Title'])
# if it has tracks print them too
for j in range(obj['Number of Tracks']):
print(obj['Tracks'][j])
# line change
print()
# call functions
res=toJSON()
JSONtoTable(res)

OUTPUT:


Please upvote if you like my answer and comment below if you have any queries or need any further explanation.
Python - Compose a program script that accepts input for album data for 3 albums including Artist,...