If you're sure, that you have only 3 columns everywhere, you can use item unpacking.
No use of
I corrected the assignment in #11 of your code.
If you want to assign a value to a key, it looks like this:
The datetime object is for example immutable. The values of a dict, don't need to be hashable.
And since Python 3.6 we've got the implementation detail, that dicts keeps the order.
Since Python 3.7 it's in the language specification and a guarantee.
If you test your code with older Python version, you'll get scrambled results.
Previously dicts didn't keep the order. In some versions they used an algorithm to scramble it.
If you want to write code for older Python versions, you have to know it.
In this case you can use
with open('playlist.txt', 'r') as csv_file: playlist_dict = {} 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)You can make it shorter.
No use of
splitlines
, because the csv_reader does it indirect.I corrected the assignment in #11 of your code.
If you want to assign a value to a key, it looks like this:
some_dict = {} a_key = 'my_key' some_value = 42 some_value = (1,2,3) # could be a tuple some_value = [1,2,3] # could be a list some_value = {'foo': 'bar'} # or a dict some_value = {1,2,3} # could be a set # assignment some_dict[a_key] = some_value # in this case the name was last overwritten by a setKeys could be only hashable objects. This means you could not use mutable mappings/sequences as key.
The datetime object is for example immutable. The values of a dict, don't need to be hashable.
And since Python 3.6 we've got the implementation detail, that dicts keeps the order.
Since Python 3.7 it's in the language specification and a guarantee.
If you test your code with older Python version, you'll get scrambled results.
Previously dicts didn't keep the order. In some versions they used an algorithm to scramble it.
If you want to write code for older Python versions, you have to know it.
In this case you can use
collections.OrderedDict
.
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!