Это часть большого проекта кода, но я застрял в этой части проекта.
Я ищу магазин с полками для хранения различных словарей. Эти имена зависят от того, насколько далеко я нахожусь в цикле for, который основан на пользовательском вводе (выбирает, сколько наборов данных существует)
В данный момент мой код выдает ошибку
import shelve
#opening a data file
f = shelve.open('data', flag='c', writeback=True)
#getting some the number for the loops (which I call data sets)
numDataPoints = int(input('Enter the number of data sets:\n'))
#Has the loop, starting at 1, ending with the number entered into numDataPoints
for i in range(1, numDataPoints +1):
#reopening just in case :-)
f = shelve.open('data', flag='c', writeback=True)
#getting a value which I want to store later
val1 = float(input("Enter independent value for data point " + str(i) +": "))
#getting another number for the next loop within this data point loop
rep = int(input('Enter the number of repeats: '))
#saving the data I already have to a dictionary (not in.a shelve file)
diction = {'val1': val1, 'reps': rep}
#starting the new loop for each repeat, similar way to before
for x in range(1, rep+1):
#find out another user input value which I want to get stored
val2 = float(input('Enter dependent variable value for repeat number ' + str(x) + ':'))
#saving this value to the dictionary with the repetition number as the key
diction['rep'+str(x)] = val2
#This is where the problem is as I am trying to save all this information that I have stored in the 'diction' dictionary into a shelve file under a dictionary in the shelve file, so that I can call it back later. The 'i' refers back to the first for loop
f[str(locals()["group".format(str(i))])] = diction
f.sync()
#This is just to test and see if they all saved properly
for key in f:
print(f[key])
f.close()
Это ошибка с этим:
Traceback (последний вызов был последним):
File "/Users/thomasholland/PycharmProjects/sdak_local/test.py", line 23, in <module>
f[str(locals()["group".format(str(i))])] = diction
KeyError: 'group1'
Я ожидаю, что словарь (словарь) будет сохранен на полке, как если бы я входил в него нормально. Я думаю, что проблема в том, что group1 недоступна для редактирования, но я не знаю, как сделать ее доступной.