Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In Python, Create a function called \'generateStudentDictionary\' that accepts a

ID: 3796397 • Letter: I

Question

In Python, Create a function called 'generateStudentDictionary' that accepts a file name. Invoke the function with the file name ''. This file can be found on the resources page. (As always, your function should confirm that the file opens okay by checking for an IOError and for a FileNotFoundError.) This is a comma-separated file like the one in the previous question. The first field on each line represents a student ID. The second field represents a student name. Create an empty dictionary. For all of the students in the file, add a new entry to the dictionary in which the key is the student ID and the value for that key is the student name. For example, the first line in the file is: 11111,Homer Grainger,47,773-444-5555,FT . Therefore the first entry in your dictionary will be the key 11111 and its value will be Homer Grainger. The function should return the dictionary.

Here is what the output would look like:

generate StudentDictionary student info. csv 222222 Marge Potter 33333 Moe Longbottom '11111' Homer Grainger 44444 Monty Malfoy'

Explanation / Answer

def generateStudentDictionary(filename):
student_dict = {}
try:
with open(filename) as fp:
for line in fp:
record = line.split(",")
if len(record) < 2:
print "Invalid entry: " + line
continue
elif record[0].isspace() or record[1].isspace():
print "Invalid entry: " + line
continue
else:
student_dict[record[0]] = record[1]
except IOError as err:
print "Faced some io error while reading file!"

return student_dict

student_dict = generateStudentDictionary("student_info.csv")
print student_dict

# url for code https://goo.gl/iUMpnq

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote