2014-10-08 23 views
-3
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() 

我不知道如何添加字典'Stuff'。如果有人能告诉我如何使用shelve附加字典,因为我不知道。我使用搁置追加并添加到字典,但它不会工作

+0

你能否提供比*“它不起作用”*更清晰的问题描述?此外,您的示例将不会运行,因为缺少引号。 – jonrsharpe 2014-10-08 10:19:25

+1

'db ['Stuff']。append(Dictionary)'只有在db ['Stuff']'是一个列表时才有效。显然,如果你说它不起作用,情况并非如此。您可以简单地将货物分配给您的货架,例如'db ['Stuff'] = Dictionary'。这包括在官方文档中,请在对SO提出问题之前,先阅读并在交互式口译员中自行尝试。你的代码还有其他一些问题:缺少'db.close'上的父类,'Dictionary'是一个dict的坏名字 - 不要用大写字母开始变量名,并且使用反映变量内容的名字,而不是他们的类型。 – l4mpi 2014-10-08 10:31:27

回答

0
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()