Python Forum
Read csv file, parse data, and store in a dictionary
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Read csv file, parse data, and store in a dictionary
#5
My mistake.

In line number 9:
playlist_dict[dt].append((song, artist))
# the key dt does not exist
# no list behind
to...

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 list
So 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!
Reply


Messages In This Thread
RE: Read csv file, parse data, and store in a dictionary - by DeaD_EyE - Nov-26-2019, 03:33 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Connecting to Remote Server to read contents of a file ChaitanyaSharma 1 219 May-03-2024, 07:23 AM
Last Post: Pedroski55
  Help with to check an Input list data with a data read from an external source sacharyya 3 466 Mar-09-2024, 12:33 PM
Last Post: Pedroski55
  Matching Data - Help - Dictionary manuel174102 1 425 Feb-02-2024, 04:47 PM
Last Post: deanhystad
  Recommended way to read/create PDF file? Winfried 3 2,945 Nov-26-2023, 07:51 AM
Last Post: Pedroski55
  parse json field from csv file lebossejames 4 796 Nov-14-2023, 11:34 PM
Last Post: snippsat
  python Read each xlsx file and write it into csv with pipe delimiter mg24 4 1,552 Nov-09-2023, 10:56 AM
Last Post: mg24
  read file txt on my pc to telegram bot api Tupa 0 1,160 Jul-06-2023, 01:52 AM
Last Post: Tupa
  parse/read from file seperated by dots giovanne 5 1,158 Jun-26-2023, 12:26 PM
Last Post: DeaD_EyE
  Formatting a date time string read from a csv file DosAtPython 5 1,382 Jun-19-2023, 02:12 PM
Last Post: DosAtPython
  How do I read and write a binary file in Python? blackears 6 7,014 Jun-06-2023, 06:37 PM
Last Post: rajeshgk

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020