My mistake.
In line number 9:
If there is an song/artist with the same date, the old one is just overwritten.
If you expect this, the easiest way is to use a defaultdict.
In line number 9:
playlist_dict[dt].append((song, artist)) # the key dt does not exist # no list behindto...
playlist_dict[dt] = (song, artist)If you expect songs/artist with the same date, then the value should be a list.
from collections import defaultdict playlist_dict = defaultdict(list) # not existing keys, return an empty list # which could be modified playlist_dict['this key does not exist'].append(42) # <-- returns an empty list, which is already assigned to the key # now the key 'this key does not exist' exists. playlist_dict['this key does not exist'].append(43) # <-- adding next object to the existing listSo you can decide. Just assign a tuple with song/artist to the key.
If there is an song/artist with the same date, the old one is just overwritten.
If you expect this, the easiest way is to use a defaultdict.
import csv from collections import defaultdict with open('playlist.txt', 'r') as csv_file: playlist_dict = defaultdict(list) reader = csv.reader( csv_file, quotechar='"', delimiter=',', quoting=csv.QUOTE_ALL, skipinitialspace=True ) for timestamp, song, artist in reader: dt = datetime.strptime(timestamp, '%B %d, %Y %I:%M %p') playlist_dict[dt].append((song, artist)) print(playlist_dict)
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
All humans together. We don't need politicians!