Squenson:
I vaguely understand! How could I copy and save the values of innerdict to make them independent of the dictionary?
Wavic:
I implemented the line you gave me and it worked for a second, but now it pulls up a ValueError. Here's the updated code:
Hi Wavic,
I've almost gotten it!
The only problem left with it is that 'number' in the inner dictionary has the wrong value.
The line of code you helped me with is from index 1, everything is right except for the value of 'number', which is given the
Is there a way to get all of the indices excluding parts[1]?
Here's the code:
I vaguely understand! How could I copy and save the values of innerdict to make them independent of the dictionary?
Wavic:
I implemented the line you gave me and it worked for a second, but now it pulls up a ValueError. Here's the updated code:
def reader(filename): file_reader = open(filename) results = [] innerdict = {} outerdict = {} for line in file_reader: parts = line.split(" ") parts = line.replace("\n", "") line_tuple = (int(parts[0]), parts[1], int(parts[2]), int(parts[3]), float(parts[4])) key = line_tuple[1] # innerdict["number"] = line_tuple[0] # innerdict["grade"] = line_tuple[2] # innerdict["total"] = line_tuple[3] #innerdict["weight"] = line_tuple[4] outerdict[parts[1]] = dict(zip(['number','grade','total','weight'], parts[1:])) #outerdict[key] = innerdict return outerdict file_reader.close()And here's the error:
Error:Traceback (most recent call last):
File "Reader.py", line 81, in <module>
print(reader("sample.cs1301"))
File "Reader.py", line 57, in reader
line_tuple = (int(parts[0]), parts[1], int(parts[2]), int(parts[3]), float(parts[4]))
ValueError: invalid literal for int() with base 10: 'a'
Command exited with non-zero status 1
(Dec-15-2017, 11:04 PM)wavic Wrote: Hm! Is it allowed dictionary comprehension?
Open the file and read it with the readlines method. This will create a list of the lines. Create an empty dict for the final result. Iterate over the lines and for each line: split the line the_dict[splited_line[0]] = dict(zip(['number','grade','total','weight'], splited_line[1:]))Done.
Of course, you will have to turn the digits from string to integers somewhere in the loop
Hi Wavic,
I've almost gotten it!
The only problem left with it is that 'number' in the inner dictionary has the wrong value.
The line of code you helped me with is from index 1, everything is right except for the value of 'number', which is given the
parts[1]when that is the key for the larger dictionary.
Is there a way to get all of the indices excluding parts[1]?
Here's the code:
def reader(filename): file_reader = open(filename) results = [] innerdict = {} outerdict = {} for line in file_reader: line = line.replace("\n", "") parts = line.split(" ") line_tuple = (int(parts[0]), parts[1], int(parts[2]), int(parts[3]), float(parts[4])) key = line_tuple[1] # innerdict["number"] = line_tuple[0] # innerdict["grade"] = line_tuple[2] # innerdict["total"] = line_tuple[3] #innerdict["weight"] = line_tuple[4] outerdict[parts[1]] = dict(zip(['number','grade','total','weight'], parts[1:])) #outerdict[key] = innerdict return outerdictHere's the output. It's so close!
Output:{'exam_1': {'grade': '95', 'total': '100', 'weight': '0.5', 'number': 'exam_1'}, 'test_1': {'grade': '90', 'total': '100', 'weight': '0.25', 'number': 'test_1'}, 'assignment_1': {'grade': '85', 'total': '100', 'weight': '0.25', 'number': 'assignment_1'}}