Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Analyze #218

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file not shown.
Binary file not shown.
Binary file not shown.
49 changes: 49 additions & 0 deletions submissions/mishc/analyse.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#1. Write a script to identify the number of songs in the file.
#2. Write a script that identify the number of songs in the key of E.
#3. Count the occurrences of values in a specified column (e.g., artist names) and determine the most common value.

import sys
import os
def analysis(csvObject):
try:
header = next(csvObject)

analyse_val={"num_songs":0,"num_songs_E":0, "artist_count":0, "artist_names":{}}

for row in csvObject:
number_of_songs(row,header,analyse_val)
most_common_artist_name(row, header, analyse_val)

print_results(analyse_val)

except Exception as e:
print("Error in the 'analysis' function:", e)
exc_type, exc_obj, exc_tb = sys.exc_info()
fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
print(exc_type, fname, exc_tb.tb_lineno)


def number_of_songs(row, header,analyse_val):
index_key = header.index('key')
analyse_val['num_songs'] += 1
key=row[index_key]
if key=="E":
analyse_val['num_songs_E'] += 1
def most_common_artist_name(row, header, analyse_val):
index_name = header.index('artist(s)_name')
artist_name=row[index_name]
artist_names=artist_name.split(',')
for artist_name in artist_names:
if artist_name not in analyse_val['artist_names']:
analyse_val['artist_names'][artist_name]=1
else:
analyse_val['artist_names'][artist_name]+=1


def print_results(analyse_val):

print("Number of songs:", analyse_val['num_songs'])
print("Number of songs in key E:", analyse_val['num_songs_E'])

sorted_dict = sorted(analyse_val['artist_names'].items(), key=lambda item:item[1], reverse=True)
print("Most common artist name:", next(iter(sorted_dict))[0])
12 changes: 12 additions & 0 deletions submissions/mishc/delete_double_rows.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
def delete_double_rows(csvObject):

unique_lines = []
encountered_lines = set()

for row in csvObject:
row_tuple = tuple(row)

if row_tuple not in encountered_lines:
unique_lines.append(row)
encountered_lines.add(row_tuple)
return unique_lines
27 changes: 27 additions & 0 deletions submissions/mishc/index.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import csv, sys,os
from delete_double_rows import delete_double_rows
import analyse as anal


try:
with open('spotify-2023.csv', 'r', encoding='ISO-8859-1') as file:
csv_reader = csv.reader(file)
unique_lines=delete_double_rows(csv_reader)
with open('spotify-2023.csv', 'w', newline='') as file:
writer = csv.writer(file)
writer.writerows(unique_lines)
with open('spotify-2023.csv', 'r', encoding='ISO-8859-1') as file:
csv_reader = csv.reader(file)
anal.analysis(csv_reader)


except FileNotFoundError:
print("File not found")
except csv.Error as e:
print(f"CSV Error: {type(e).__name__} - {str(e)}")
except Exception as e:
print(f"An error occurred: {type(e).__name__} - {str(e)}")
exc_type, exc_obj, exc_tb = sys.exc_info()
fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
print(exc_type, fname, exc_tb.tb_lineno)

954 changes: 954 additions & 0 deletions submissions/mishc/spotify-2023.csv

Large diffs are not rendered by default.