def load_method(self, inp_list, file):
self.DB.global_row = -1
# clear the widgets and listbox
self.DB.clear_test_list()
for item in self.DB.inp_dict:
del item
inp_list.delete(0, 'end')
# THIS IS THE RELEVANT PART FOR THIS QUESTION
[self.DB.test_dict, self.DB.inp_dict] = json.load(file)
counter = 0
for key in self.DB.test_dict: # change the keys to be integer instead of strings
self.DB.test_dict[counter] = self.DB.test_dict[key]
del self.DB.test_dict[key]
counter += 1
I’m sure there is a correct way to do it, does anyone know how?
>Solution :
You’d have to create a new dictionary from your existing one. A simple pythonic way of achieving this would be by changing the code as follows:
def load_method(self, inp_list, file):
self.DB.global_row = -1
# clear the widgets and listbox
self.DB.clear_test_list()
for item in self.DB.inp_dict:
del item
inp_list.delete(0, 'end')
# THIS IS THE RELEVANT PART FOR THIS QUESTION
[self.DB.test_dict, self.DB.inp_dict] = json.load(file)
self.DB.test_dict = {int(idx): self.DB.test_dict[key] for idx, key in enumerate(d.keys())}