import re out='''1 assignment_1 85 100 0.25 2 test_1 90 100 0.25 3 exam_1 95 100 0.5''' innerdict = {} outerdict = {} parts_1 = out.split("\n") for line in parts_1: parts = line.split(" ") line_tuple = (int(parts[0]), parts[1], int(parts[2]), int(parts[3]), float(parts[4])) key = line_tuple[1] outerdict[key] = dict(innerdict) outerdict[key]={'Total':'{0}'.format(parts[3]),'Number':'{0}'.format(parts[0]),'Grade':'{0}'.format(parts[2]),'Weight':'{0}'.format(parts[4])} print(outerdict)
output:
Output:{'assignment_1': {'Total': '100', 'Number': '1', 'Grade': '85', 'Weight': '0.25'}, 'test_1': {'Total': '100', 'Number': '2', 'Grade': '90', 'Weight': '0.25'}, 'exam_1': {'Total': '100', 'Number': '3', 'Grade': '95', 'Weight': '0.5'}}
You can try this.