Question
.cs1301file= 1 assignment_1 85 100 0.25
2 test_1 90 100 0.25
3 exam_1 95 100 0.5
My code that I had for this :
def reader(filename):
bigDict={}
numberDict={}
totalDict={}
weightDict={}
gradeDict={}
lineList=[]
splitString=[]
openFile=open(filename,"r")
for line in openFile:
lineList.append(line)
for i in range(0,len(lineList)):
splitString=lineList[i].replace(" "," ")
splitString=splitString.split(" ")
bigDict[splitString[1]]={}
bigDict[splitString[1]]["number"]=int((splitString[0]))
bigDict[splitString[1]]["grade"]=int((splitString[2]))
bigDict[splitString[1]]["total"]=int((splitString[3]))
bigDict[splitString[1]]["weight"]=float((splitString[4]))
bigDict[splitString[2]]={}
bigDict[splitString[2]]["number"]=int((splitString[0]))
bigDict[splitString[2]]["grade"]=int((splitString[2]))
bigDict[splitString[2]]["total"]=int((splitString[3]))
bigDict[splitString[2]]["weight"]=float((splitString[4]))
bigDict[splitString[3]]={}
bigDict[splitString[3]]["number"]=int((splitString[0]))
bigDict[splitString[3]]["grade"]=int((splitString[2]))
bigDict[splitString[3]]["total"]=int((splitString[3]))
bigDict[splitString[3]]["weight"]=float((splitString[4]))
openFile.close()
return bigDict
Coding Problem 4.5.11 (Advanced) (External resource) (0.0/1.0 points) Reader.py Submit Run Grades Reset ???e a Tunction ?? ???????? ??? ????? ??? ?????????, 2 ta filename as a string corresponding to a .cs1301 file, 3 #and reads in that .cs1301 file. 4 #Each line of the .cs1301 file will have five itemseach 5 #separated by a space: the first, third, and fourth will 6 #represent integers, the second will be a string, and the 7 #fifth will represent a float. (Note: when reading the 8 #filc, these will all be strings you can assume each of 9 #these strings can be converted to the corresponding data 10 #type, however.) #The function should return a dictionary of dictionaries 11 #representing the file contents. 12 #The keys of the top-level dictionary should be the 13 #assignment names. Then, the value for each of those keys 14 #should be a dictionary with four keys: "number", "grade", 15 #"total" and "weight". The values corresponding to each of 16 #those four keys should be the values from the file, 17 #converted to the corresponding data types (ints or floats). 18 #For example, if the input file read: 19 # 1 exam 1 98 109 8.6 20 # 2 exam 2 95 100 8.4 21 #Then reader would return this dictionary of dictionaries: 22 # d'exam_1": {"number": 1, "grade" : 90, "total": 100, "weight": 8.6) 23 # "exan_2": "number": 2, "grade" : 95, "total": 100, "weight": 0.43 24 #Hint: Althoueh the end result is pretty different, this 25 #should only dictate a minor change to your original 26 #Problem 4.4.3 code! 27 write your function here: 28 #Below are some lines of code that will test your function. 29 #You can change the value of the variable(s) to test your 30 #function with different inputs. 31 #If your function works correctly, this will originally 32 print (although the order of the keys may vary): 33 #( "assignment_i': {'total': 160, 'number': 1, 'grade' : 85, 'weight': 8.25), 'test_i': {'total': 100, 'number': 2, 'grade': 90, 'weight': 0.25}, exam_?' : 'total': 100, 'number': 3, 'grade': 95, 'weight': 0.5}} 34 print reader("sample.cs1301")) {'assignment_1': {'number': 1, 'weight': 0.25, 'total': 180, 'grade': 85), '8': {'number': 1, 'weight': 0.25, 'total': 168, 'grade': 85), '100': {'numbe ': 1, 'weight': 0.25, 'total': 100, 'grade': 85}}
Explanation / Answer
def reader(filename): d = {} with open(filename, 'r') as f: for line in f: words = line.strip().split() d[words[1]] = {"number": int(words[0]), "grade": int(words[2]), "total": int(words[3]), "weight": float(words[4])} return d