How to create a dictionary from a CSV file, appending multiple values to three keys?

Question:

I need to be able to append multiple values from a CSV file to a dictionary that has three keys. The keys are morning, midday, and night. The values should come from each row of the CSV file.

Ideally, it should look like below.

I can’t use numpy or csv modules, so those options are not available. It has to work with no imports, and I’m unsure with dictionaries and how to approach this to get the output I need.

Here is a sample of what the CSV file looks like:

{
    'morning': {[5, 5, 10, 17, 20, 21]},
    'midday': {[10, 20, 25, 15, 8, 3]},
    'night': {[3, 5, 2, 7, 15, 29]}
}

Here is my code:

time_list = []
time_dict = {}

with open('stats.csv', 'r') as data_file:
    headers = data_file.readline()

    for line in data_file:
        Time, VS = line.split(',')
        time_list.append(int(VS))
        time_dict[Time] = time_dict.get(Time, 0) + int(VS)

Appending to the list produces each value like it should, i.e.:

[2, 3, 4, 5, 6, 7, 8]

But for the dictionary, it does not show each value individually for the key it is attached to. Instead, it will take all the values for each key and add them together.

Printing the dictionary shows the following:

{'morning': 2097, 'midday': 1240, 'night': 1533}

I’m unsure of how to approach this to get the dictionary to look like the following:

{
    'morning':{[5, 5, 10, 17, 20, 21]},
    'midday': {[10, 20, 25, 15, 8, 3]},
     'night': {[3, 5, 2, 7, 15, 29]}
}

Note: Many of the answers I have found use the csv module, which I unfortunately cannot use for this. I have to use no imports for this solution.

Also keytype of the dictionary must be int so I can peform math operations on the dictionary to find out the max, min, average etc

Asked By: wctn2022

||

Answers:

How about changing your strategy and doing it like this:

morning_list= []
midday_list= []
night_list= []
time_dict = dict()

with open("stats.csv", "r") as data_file:
    headers = data_file.readline()

    for line in data_file:
        Time, VS = line.split(",")
        if Time == "morning":
           morning_list.append(int(VS))
        elif Time == "midday":
           midday_list.append(int(VS))
        elif Time == "night":
           night_list.append(int(VS))

time_dict["morning"] = morning_list
time_dict["midday"] = midday_list
time_dict["night"] = night_list
Answered By: ImotVoksim

I would do it this way:

import csv 

with open(fn) as csv_in:
    reader=csv.reader(csv_in)
    header=next(reader)
    data={}
    for t,vs in reader:
        data.setdefault(t, []).append(int(vs))

From comment "I can’t use imports":

with open(fn) as csv_in:
    header=next(csv_in)
    data={}
    for t,vs in (line.split(',') for line in csv_in):
        data.setdefault(t, []).append(int(vs))

From comment "The data is in the eighth column":

With a slight modification (and assuming Python 3) you can set *vs so that vs accepts a variable number in the expansion:

with open(fn) as csv_in:
    header=next(csv_in)
    data={}
    for t,*vs in (line.split(',') for line in csv_in):
        data.setdefault(t, []).append(int(vs[7]))

Given this file:

Time,VS
morning,1,2,3,4,5,6,7,80
morning,1,2,3,4,5,6,7,81
midday,1,2,3,4,5,6,7,90
midday,1,2,3,4,5,6,7,91
night,1,2,3,4,5,6,7,100
night,1,2,3,4,5,6,7,101

Prints:

>>> data
{'morning': [80, 81], 'midday': [90, 91], 'night': [100, 101]}
Answered By: dawg
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.