2014-10-08 40 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()