In: Computer Science
PYTHON LANGUAGE
Write the data in variable artist_songs into a csv file, 'songs.txt', where the first column is singer name and second column is a song name. Each line should have a singer and a song name. Use "Name" and "Song" as headers. Do not include double quotation marks (") in your CSV but you should include apostrophes where necessary (for example, for the song "We Don't Talk Anymore").
In [110]:
artist_songs = {
'Taylor Swift': ['Love Story', 'You need to Calm Down'],
'Charlie Puth': ['Attention', "We Don't Talk Anymore", 'Change']
}
Complete code in python3:-
import csv
# Dictionary that is containing data
# Singer name and songs
artist_songs = {
'Taylor Swift': ['Love Story', 'You need to Calm Down'],
'Charlie Puth': ['Attention', "We Don't Talk Anymore",
'Change']
}
# 'header' list is containing the column name.
header = ['Name', 'Song']
# file name
filename = 'songs.csv'
# opening the csv file in write mode.
with open(filename, 'w') as file:
# Creating file writer object to write.
fileWriter = csv.writer(file)
# writting header into the file
fileWriter.writerow(header)
# writting data into file.
for key in artist_songs.keys():
songs = artist_songs[key]
for song in songs:
fileWriter.writerow([key, song])
Screenshot of file:-