Karan Thakkar
Karan Thakkar

Reputation: 1027

Python: file contents to dictionary

Write a function that accepts a filename(string) of a CSV file which contains the information about student's names and their grades for four courses and returns a dictionary of the information. The keys of the dictionary should be the name of the students and the values should be a list of floating point numbers of their grades. For example, if the content of the file looks like this:

Mark,90,93,60,90
Abigail,84,50,72,75
Frank,46,83,53,79
Yohaan,47,77,74,96

then your function should return a dictionary such as:

out_dict = {'Frank': [46.0, 83.0, 53.0, 79.0],
            'Mark': [90.0, 93.0, 60.0, 90.0],
            'Yohaan': [47.0, 77.0, 74.0, 96.0],
            'Abigail': [84.0, 50.0, 72.0, 75.0]}

This is my code:

def dict_from_file (file_name):
    file_pointer = open(file_name, 'r')
    data = file_pointer.readlines()
    print (data)
    output_dict = {}
    for line in data:

    file_pointer.close()
    return (output_dict)

#Main Program
file_name = input("Enter the exact file name with its extension (.i.e., .txt): ")
result = dict_from_file (file_name)
print (result)

As you can see the statements are missing inside the for loop. The problem is I cannot find any logic to first take the input from the file and paste it in a dictionary. And if I plan to extract each line once how will I add it to the dictionary with name as the key and the four numbers as the values?

Upvotes: 2

Views: 581

Answers (2)

srowland
srowland

Reputation: 1705

I suggest you use csv reader:

import csv
with open(file_name, 'rb') as csvfile:
    csv_reader = csv.reader(csvfile)
    for row in csv_reader:
        output_dict[row[0]] = [float(n) for n in row[1:]]

Something like that should work if correctly inserted into your for loop. I haven't tested the code, but it shouldn't be too far off.

Upvotes: 2

idjaw
idjaw

Reputation: 26578

To strictly focus on your code:

If you print out what you've read, you will notice you have this:

['Mark,90,93,60,90\n', 'Abigail,84,50,72,75\n', 'Frank,46,83,53,79\n', 'Yohaan,47,77,74,96']

So, when you are iterating through each item in your list, to keep this example simple, you can pretty much extract the first item, which will now be your key, and then take the part of the list that excludes the name and cast each item to a float.

So, you pretty much are looking for:

l = line.split(',')
output_dict[l[0]] = map(float, l[1:])

What is happening up there is that you are taking each item you are iterating over, making it a list, by splitting on ','. Then you are going to take the first item in the list and make that your key. Then to assign your value, you have to make use of the map method, which will map out each item in your list to a float.

You are going to do that operation for each line being read.

So, finally, you will have:

def dict_from_file (file_name):
    file_pointer = open(file_name, 'r')
    data = file_pointer.readlines()
    print (data)
    output_dict = {}
    for line in data:
        l = line.split(',')
        output_dict[l[0]] = map(float, l[1:])
    file_pointer.close()
    return output_dict

#Main Program
result = dict_from_file ('stuff.txt')
print (result)

Running that code, will give you:

{'Frank': [46.0, 83.0, 53.0, 79.0], 'Yohaan': [47.0, 77.0, 74.0, 96.0], 'Abigail': [84.0, 50.0, 72.0, 75.0], 'Mark': [90.0, 93.0, 60.0, 90.0]}

Upvotes: 5

Related Questions