gpt4 book ai didi

python - 在运行时 append 到字典

转载 作者:太空宇宙 更新时间:2023-11-03 19:35:02 26 4
gpt4 key购买 nike

我有一个名为 OpenAccount() 的函数,它接收用户的详细信息并将其 append 到我的数据库字典中。

我有一个数据库文件(模块),它导入到我的函数文件中。

我有一个名为 AppendRecord(key,**dictvalues) 的函数,它将值 append 到我的数据库文件中。

但是,我无法调用该函数并在运行时获取值来 append 。它适用于硬编码值。我正在发布代码。请帮助我。

def AppendRecord(key, **dictvalues):
salesDept[key] = dict(dictvalues)

对硬编码值的调用是......

AppendRecord('Jill',Name='Jill', Acctype='Savings')

现在,当我尝试从用户那里获取所有值和 key 时,我无法调用该函数。我只是 Python 的初学者,所以请原谅我的任何错误:

编辑后的代码:

#!/usr/bin/python

import os       #This module is imported so as to use clear function in the while-loop
import DB #Imports the data from database DB.py

def AppendRecord(key, **dictvalues):
DB.Accounts[key] = dict(dictvalues)

def OpenAccount(): #Opens a new a/c and appends to the database data-base.

while True:

os.system("clear") #Clears the screen once the loop is re-invoked

#Parameters taken from the user at Run-time so as to make entries in the database and append them


print '\n','Choose an Account Type'

print '\n','\n1)Savings Account','\n2)Current Account'

choice = input('Enter an optin: ')

if choice == 1:

name = raw_input('\nEnter a name: ')
depo = input('\nEnter amount(Rs.): ')
key = raw_input('\nEnter an alphanumeric-id: ')
acc= raw_input('\nEnter the Account-Type: ')

AppendRecord(key,Name=name,Initial_deposit=depo,Acctype=acc)

编辑1:我得到的错误是。

 File "/usr/lib/python2.5/shelve.py", line 124, in __setitem__
self.dict[key] = f.getvalue()
TypeError: 'int' object does not support item assignment

编辑2:

以下是DB.py数据库文件源代码。

#!/usr/bin/python

import shelve #Module:Shelve is imported to achieve persistence

Victor = {'Name':'Victor Hughes','Acctype':'Savings'}
Xavier = {'Name':'Xavier Bosco','Acctype':'Savings'}
Louis = {'Name':'Louis Philip','Acctype':'Current'}
Beverly = {'Name':'Beverly Dsilva','Acctype':'Current'}

Accounts = shelve.open('shelfile.shl') #Accounts = {}

Accounts['Louis']= Louis
Accounts['Beverly']= Beverly
Accounts['Xavier']= Xavier
Accounts['Victor']= Victor

Accounts.close()

最佳答案

您的问题是 DB 模块在写入之前关闭了架子。去掉最后一行,它就会正常工作。

您还需要提供一个函数来关闭它或手动执行此操作。

#!/usr/bin/python

import shelve #Module:Shelve is imported to achieve persistence


Accounts = 0

Victor = {'Name':'Victor Hughes','Acctype':'Savings'} #???
Xavier = {'Name':'Xavier Bosco','Acctype':'Savings'}
Louis = {'Name':'Louis Philip','Acctype':'Current'}
Beverly = {'Name':'Beverly Dsilva','Acctype':'Current'}


def open_shelf(name='shelfile.shl'):
global Accounts
Accounts = shelve.open(name) #Accounts = {}
# why are you adding this every time? is this just debugging code?
Accounts['Louis']= Louis
Accounts['Beverly']= Beverly
Accounts['Xavier']= Xavier
Accounts['Victor']= Victor


def close_shelf():
Accounts.close()

现在,您需要在写入之前调用 DB.open_shelf() 函数,并在完成后调用 DB.close_shelf 函数。我建议在程序的开头和结尾分别调用它们。

您会注意到,这实际上只是包装了 shelve 并添加了几乎为零的值。我会废弃整个 DB 模块,直接使用 shelve

关于python - 在运行时 append 到字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4007522/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com