Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我正在使用shelve追加和添加到字典中,但它不会';行不通_Python_Python 3.x_Shelve - Fatal编程技术网

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