在运行时附加到字典

2024-05-17 15:09:45 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

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

但是,我不能在运行时调用函数并接受要附加的值。 它可以很好地与硬编码的值一起工作密码。请帮帮我。在

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

对硬编码值的要求是。。。在

^{pr2}$

现在,当我试图从用户那里接收所有值和键时,我无法调用函数。我只是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>编辑:

以下是数据库数据库文件源代码。在

#!/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()

Tags: 文件thetokeyname数据库inputaccount
1条回答
网友
1楼 · 发布于 2024-05-17 15:09:45

您的问题是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。在

相关问题 更多 >