Python 我正在使用shelve追加和添加到字典中,但它不会';行不通
我不知道如何添加字典Python 我正在使用shelve追加和添加到字典中,但它不会';行不通,python,python-3.x,shelve,Python,Python 3.x,Shelve,我不知道如何添加字典“Stuff”。如果有人能告诉我如何使用shelve添加词典,因为我不知道。你能提供一个比“它不起作用”更清晰的问题描述吗?另外,您的示例将不会运行,因为缺少引号。db['Stuff'].append(Dictionary)仅当db['Stuff']是一个列表时才起作用。显然,如果你说它不起作用,情况就不是这样了。您可以简单地将内容分配给您的搁置,例如db['Stuff']=Dictionary。这是官方文件中的内容,在提问之前,请在交互式口译员中阅读并亲自尝试。您的代码还存
“Stuff”
。如果有人能告诉我如何使用shelve
添加词典,因为我不知道。你能提供一个比“它不起作用”更清晰的问题描述吗?另外,您的示例将不会运行,因为缺少引号。db['Stuff'].append(Dictionary)
仅当db['Stuff']
是一个列表时才起作用。显然,如果你说它不起作用,情况就不是这样了。您可以简单地将内容分配给您的搁置,例如db['Stuff']=Dictionary
。这是官方文件中的内容,在提问之前,请在交互式口译员中阅读并亲自尝试。您的代码还存在多个其他问题:您缺少db.close
上的paren,而Dictionary
对于dict来说是个坏名字-不要以大写字母开头变量名,使用反映变量内容的名称,而不是它们的类型。
import shelve
name = input('Please enter your name: ')
reg = input(Please enter the registration plate: ')
speed = input(Please enter the speed: ')
Dictionary = {name:{reg:speed}}
db = shelve.open('Data')
db['Stuff'].append(Dictionary)
db.close
f = shelve.open('Data', 'r')
print(f['Stuff'])
f.close()
import shelve
name = input('Please enter your name: ')
reg = input('Please enter the registration plate: ')
speed = input('Please enter the speed: ')
Dictionary = {name:{reg:speed}}
db = shelve.open('Data')
db['Stuff'].append(Dictionary)
db.close
f = shelve.open('Data', 'r')
print(f['Stuff'])
f.close()