47 lines
1.0 KiB
Python
47 lines
1.0 KiB
Python
from csv import DictReader
|
|
from uuid import UUID
|
|
|
|
|
|
class Top200Song:
|
|
def __init__(self, songdict) -> None:
|
|
try:
|
|
self.id = UUID(songdict["id"])
|
|
except ValueError:
|
|
self.id = None
|
|
self.artist = songdict["artist"]
|
|
self.title = songdict["title"]
|
|
self.position = int(songdict["position"])
|
|
|
|
def __str__(self) -> str:
|
|
return f"{self.position:4d}: {self.artist} - {self.title}"
|
|
|
|
def __repr__(self) -> str:
|
|
return str(self)
|
|
|
|
|
|
def load_top2000(filename="2021.csv") -> dict[UUID, Top200Song]:
|
|
|
|
songs = {}
|
|
with open(filename) as f:
|
|
reader = DictReader(f)
|
|
|
|
for line in reader:
|
|
song = Top200Song(line)
|
|
if song.id:
|
|
songs[song.id] = song
|
|
return songs
|
|
|
|
|
|
if __name__ == "__main__":
|
|
from csv import DictReader
|
|
|
|
songs = []
|
|
with open("2021.csv") as f:
|
|
reader = DictReader(f)
|
|
for line in reader:
|
|
print(line)
|
|
songs.append(Top200Song(line))
|
|
|
|
for song in songs:
|
|
print(song)
|