我正在尝试创建简单的“accounter”,它可以显示您的现金余额和运营日志。操作很简单:接收(金额,主题)和支出(金额,主题)。它们更改平衡并将更改写入带有时间戳的日志中。应该是的
我正在尝试使用另一个.py文件作为数据库。虽然我可以导入、读取和使用数据库,但似乎找不到将更改写入数据库的方法。在任何地方都找不到我需要的解决方案。有一些方法,比如使用json和其他。但我正试图使用py文件
下面是db.py:
balance = 0
balance_logs = [
['305', 'spent','5','coke','2020-08-18 20:00'],
['202', 'spent','3','icecream','2020-08-18 20:00']
]
因此,我需要更改balance
值并在balance_logs
中追加一个列表。
另外,我可能会在db.py中添加更多的数据,所以如果我不需要使用<copy file contents fully -> modify the parts you need -> dump into file, by fully recreating it>
方法,这将是一件好事
如果我理解我的意思,我相信用json格式保存这个文件是可以的。网上有关于这方面的教程,不过我会在这里尝试总结基本知识。要从json打开并正确加载数据,可以使用:
要将数据保存到文件,可以使用:
要使用json附加数据,可以混合使用这两种方法,例如获取json中存储的字典的一部分,对其进行操作,然后再次保存。例如:
相关问题 更多 >
编程相关推荐