【问题标题】:How can we modify data that is in a shelve?我们如何修改搁置中的数据?
【发布时间】:2023-03-11 22:01:01
【问题描述】:

我使用以下代码打开了一个货架:

#!/usr/bin/python
import shelve                   #Module:Shelve is imported to achieve persistence

Accounts = 0
Victor = {'Name':'Victor Hughes','Email':'victor@yahoo.com','Deposit':65000,'Accno':'SA456178','Acctype':'Savings'}
Beverly = {'Name':'Beverly Dsilva','Email':'bevd@hotmail.com','Deposit':23000,'Accno':'CA432178','Acctype':'Current'}

def open_shelf(name='shelfile.shl'):
    global Accounts
    Accounts = shelve.open(name)          #Accounts = {}
    Accounts['Beverly']= Beverly    
    Accounts['Victor']= Victor


def close_shelf():
    Accounts.close()

我可以将值附加到搁置,但无法修改这些值。 我已经定义了一个函数 Deposit() 我想从中修改搁置中的数据。但它给了我以下错误:

Traceback (most recent call last):
  File "./functest.py", line 16, in <module>
    Deposit()
  File "/home/pavitar/Software-Development/Python/Banking/Snippets/Depositfunc.py", line 18, in Deposit
    for key in Accounts:
TypeError: 'int' object is not iterable

这是我的功能:

#!/usr/bin/python

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

def Deposit():
        while True:
                os.system("clear")              #Clears the screen once the loop is re-invoked
                input = raw_input('\nEnter the A/c type: ')
                flag=0
                for key in Accounts:
                        if Accounts[key]['Acctype'].find(input) != -1:
                                amt = input('\nAmount of Deposit: ')
                                flag+=1
                                Accounts[key]['Deposit'] += amt

                if flag == 0:
                        print "NO such Account!"    

if __name__ == '__main__':
        open_shelf()
        Deposit()
        close_shelf()

我是 Python 新手。请帮助。如果我错了,请纠正我。我需要有人对此代码的功能进行一些解释。我很困惑。

【问题讨论】:

  • 简单介绍一下python风格,因为你说你是新手,但你通常希望对函数和变量使用小写名称。正确的大小写通常保留给类。

标签: python dictionary shelve


【解决方案1】:

首先,不要对Accounts 使用全局变量,而是来回传递它。使用全局导致您的错误。像这样:

def open_shelf(name='shelfile.shl'):
    Accounts = shelve.open(name)          #Accounts = {}
    ...
    return Accounts

def close_shelf(Accounts):
    Accounts.close()


def Deposit(Accounts):
    ...   

if __name__ == '__main__':
    Accounts = open_shelf()
    Deposit(Accounts)
    close_shelf(Accounts)

其次,不要重新定义内置函数。在Deposit() 中,您将raw_input 的结果分配给名为input 的变量:

input = raw_input('\nEnter the A/c type: ')

四行之后,你尝试使用内置的input函数:

amt = input('\nAmount of Deposit: ')

但这行不通,因为input 已被重新定义!

第三,当迭代搁置的物品时,遵循以下模式:1)抓取搁置的物品,2)变异的物品,3)将变异的物品写回货架。像这样:

for key, acct in Accounts.iteritems():  # grab a shelved item
    if val['Acctype'].find(input) != -1:
        amt = input('\nAmount of Deposit: ')
        flag+=1
        acct['Deposit'] += amt          # mutate the item
        Accounts[key] = acct            # write item back to shelf

(第三条建议是根据hughdbrown 的回答调整的。)

【讨论】:

  • 谢谢。它达到了我的目的:) +1
【解决方案2】:

我想你会有更多这样的运气:

for key, val in Accounts.iteritems():
    if val['Acctype'].find(input) != -1:
        amt = input('\nAmount of Deposit: ')
        flag+=1
        val['Deposit'] += amt
        Accounts[key] = val

【讨论】:

  • 这里的模式是抓取整个搁置的项目,对其进行变异,然后将其写回。
  • Traceback(最近一次调用最后一次):文件“./Depositfunc.py”,第 26 行,在 Deposit() 文件“./Depositfunc.py”,第 12 行,在 Deposit 中Key, val in Accounts.iteritems(): AttributeError: 'int' object has no attribute 'iteritems' 这是我得到的错误,当我尝试代码时。我调用它可能是错误的。我的其余代码还可以吗?我的意思是创建的字典和函数调用?
  • 听起来你在打电话给Deposit(),而Accounts==0。这表明Deposit() 正在处理Accounts,然后通过open_shelf() 调用将其转换为字典,或者Deposit() 正在处理其他一些Accounts 对象。
  • @Steven Rumbalski:是的,我就是这个意思。谢谢。
  • @hughdbrown -- 谢谢。它符合我的目的。 :) +1
猜你喜欢
  • 2020-03-30
  • 2019-11-12
  • 1970-01-01
  • 2012-03-23
  • 2019-02-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-06
相关资源
最近更新 更多