家庭记录
假设有一个任务是帮助你的家庭记录每日的支出。每天结束时,你会记录下今天的日期、购买的物品以及花费的金额。这个信息将被保存在一个名为ledger.txt的文本文件中。编写一段Python代码来完成以下的操作:
(1)在文件末尾追加新的消费记录;
(2)需要查看当前所有的消费记录。
为了简化问题,假设每条记录只包含日期、物品和金额,使用逗号分隔。
def append_record(date, item, amount):
with open("ledger.txt", "______①______") as file:
file.write(f"{date},{item},{amount}\n")
def read_records():
with open("______②______", "r") as file:
records = file.______③______()
for record in records:
print(record, end="")
# 示例操作
append_record("2023-04-05", "Groceries", 35.20)
append_record("2023-04-06", "Internet Bill", 50)
______④______ ()