2017-09-02 211 views
1

我有一个小程序来跟踪每月余额。这工作得很好,然后我在该部分中添加以写入底部的.txt文件。我做了一些搜索,但找不到一种方法来使其工作。基本上我想继续追加到这个test.txt文件。每次我输入一个新的月份/帐户/余额时,我都会将其附加到文件中。将字典输出写入文件 - Python

另一种方法是在'exit'之后附加到test.txt文件,所以它不会在每个循环中执行它。不知道哪种方式更有效

***编辑****

这个更新代码现在创建test.txt文件,但该文件是每个循环

次要问题后的空白 - 我听说这对于使用Class来说会更好,但我没有任何想法。如果有人想证明那真的太棒了。这不是家庭作业,这是我自己的时间严格学习。

任何想法?由于

# create program to track monthly account balances 

savings = [] 

def add_accounts(date, account, balance): 

    savings.append({'date': date, 'account': account, 'balance': 
balance}) 

def print_accounts(): 
    print(savings) 

while True: 

    date = input('Enter the date, type exit to exit program: ') 
    if date == 'exit': 
     break 
    account = input('Enter the account: ') 
    balance = int(input('Enter the balance: ')) 

    add_accounts(date, account, balance) 
    print_accounts() 
with open('test.txt', 'a') as f: 
for row in savings(): 
    print (row) 
    f.write(str(savings[-1])) 
    file.close() 
+1

' “W”'覆盖文件 - 见https://stackoverflow.com/questions/4706499/how-do-you-append对一个文件 –

+0

@IzaakvanDongen。啊,是的,谢谢,应该是'一个'。这并不能解决问题。仍在print_accounts()中抛出a:for行: TypeError:'NoneType'对象不可迭代。错误 – JD2775

+0

你能否请复制错误的追溯? –

回答

1

试试这个代码(-1退出):

savings = [] 

def add_accounts(date, account, balance): 

    savings.append({'date': date, 'account': account, 'balance': 
balance}) 

def print_accounts(): 
    print(savings) 

while True: 

    date = input('Enter the date, type exit to exit program: ') 
    if date == -1: 
     break 
    account = input('Enter the account: ') 
    balance = int(input('Enter the balance: ')) 

    add_accounts(date, account, balance) 
    print_accounts() 
with open('test.txt', 'a') as f: 
    for row in savings: 
     print (row) 
     f.write(str(savings[-1])) 
     f.close() 
+0

@ jd2775这是否行得通?还有什么问题吗? –

+0

很好,非常感谢! – JD2775

+0

你可以帮我一个忙吗?当你得到时间时,运行你在这里给我看的代码?运行时,我仍然会得到一个空白输出文本文件。现在就注意到它。好奇,如果你得到相同的结果。谢谢 - – JD2775

2

的问题,你原来的代码是print_accounts()不返回任何东西,但你尝试在它的(不存在)的返回值进行操作。

下面是使用类程序的版本做出的,并与一些修改:关于类

class Account: 
    def __init__(self, id, date, balance): 
     self.id = id 
     self.date = date 
     self.balance = balance 

    def getString(self): 
     return self.id + "," + self.date + "," + str(self.balance) 

savings = [] 

def add_account(date, account, balance): 
    savings.append(Account(date, account, balance)) 

def print_accounts(): 
    for account in savings: 
     print(account.getString()) 

while True: 
    date = input("Enter the date, type exit to exit program: ") 
    if date.lower() == "exit": 
     break 
    else: 
     account = input('Enter the account: ') 
     balance = int(input('Enter the balance: ')) 
     add_account(date, account, balance) 
     print_accounts() 
     with open("test.txt", "w") as file: 
      for account in savings: 
       file.write(account.getString() + "\n") 

一些解释:该Account类有3个领域:iddatebalance。这些字段在构造函数中定义(__init__())。该类还有一个方法getString(),我用它来获取每个实例的字符串表示

总体而言,已经做了以下修改:

  • 创建一个帐户类,它作为模板保持各账户的数据对象。
  • 使用循环打印帐户并将它们写入文件。
  • date转成小写,然后检查它是否等于“退出”。这是一个小变化,但是有一个好习惯。
  • 删除f.close(),因为在使用with open()语句时没有必要。
  • 创建Account的每个实例的自定义字符串表示形式,与您将以其他方式获得的内容一致。

最后一个是通过在帐户类中定义getString方法实现的。没有什么特别的,它只是我们用来获得字符串表示的东西。

一种更好但更先进的方法是通过覆盖基础对象的__str____repr__方法。这些本质上是每个类都有的隐藏函数,但是python为我们定义的函数。这两个特定目的的目的是给出对象的字符串表示。对他们来说,默认的代码不会产生任何有意义:

<__main__.Account object at 0x0000000003D79A58> 

然而,通过重写它们,我们可以对Account实例使用str(),我们会得到我们想要的确切格式的字符串表示。修改后的类看起来像这样:

class Account: 
    def __init__(self, id, date, balance): 
     self.id = id 
     self.date = date 
     self.balance = balance 

    def __repr__(self): 
     return self.id + "," + self.date + "," + str(self.balance) 
    def __str__(self): 
     return self.__repr__() 

这也通过savings写入文件时不再需要循环:

with open("test.txt", "w") as file: 
    for account in savings: 
     file.write(account.getString() + "\n") 

假作:

with open("test.txt", "w") as file: 
    file.write(str(savings)) 

此止跌”之前工作过,因为str()会给我们你之前看到的乱码数据。但是,现在我们已经重写了方法,它工作得很好。

+0

非常感谢您花时间写出来并向我解释这一点。对我来说很有参考价值 – JD2775