The file students.csv contains a list of students registred for a graduate course in chemistry. Write a function called createStudentDict()that opens this file and populates a dictionary with all of the students. The key should be the student ID which is present in the first column. This student ID key should be recorded as a string. The value should be a list in which the first item is the student's name (should be stored as a string), the second item is the student's age (integer), and the third item is the student's current full-time occupation (string).
Here are the contents of the file :
7373 Walter White 52 Teacher
8274 Skyler White 49 Author
9651 Jesse Pinkman 27 Student
2213 Saul Goodman 43 Lawyer
6666 Gus Fring 54 Chicken Guy
8787 Kim Wexler 36 Lawyer
9999 Tuco Salamanca 53 Drug Lord
I have tried writing the function and running it? I'm a programming beginner so I'm not sure what to put here except that I've defined the function made the try/except block and the code is not running. I am not sure if there are any errors other than the index.
def createStudentDict():
try:
#Open the file
f=open("students.txt","r")
except:
#Print error message if file is not pesent
print("File is not present")
#Read the content of the file
fileContent = f.read()
#Splits the line by using the split method
lines = fileContent.split("\n")
#Create dictionary
dict = {}
#Iterate through all the line of the file
for i in range(0,len(lines)):
#Split line by using the comma as seperator
detailList = lines[i].split(',')
#Create list with the student name, age and profession
studentDetailList = [detailList[1], int(detailList[2]), detailList[3]]
#Add or update the item in the dictionary
dict.update({detailList[0]:studentDetailList})
return dict
print(createStudentDict())
The exception is :
Traceback (most recent call last):
File "C:/Users/Owner/Documents/401 python/JONES ASSIGNMENT 3.py", line 47, in <module>
print(createStudentDict())
File "C:/Users/Owner/Documents/401 python/JONES ASSIGNMENT 3.py", line 37, in createStudentDict
studentDetailList = [detailList[1], int(detailList[2]), detailList[3]]
IndexError: list index out of range
This is the error I'm receiving. This is the expected output Invoking the function like so: print(createStudentDict()) Should generate the following output:
{'7373': ['Walter White', 52, 'Teacher'], '8274': ['Skyler White', 49, 'Author'], '9651': ['Jesse Pinkman', 27, 'Student'], '2213': ['Saul Goodman', 43, 'Lawyer'], '6666': ['Gus Fring', 54, 'Chicken Guy'], '8787': ['Kim Wexler', 36, 'Lawyer'], '9999': ['Tuco Salamanca', 53, 'Drug Lord']}