import pickle
account_file=open("account.db","rb") #打开文件 account_dict=json.loads(account_file.read().decode("utf-8")) #以json加载文件 #print (account_dict) account_file.close() #print(type(account_dict['1000']['balance'])) account_dict['1000']['balance'] -= 500 #取字典中balance然后减500 print (account_dict)
f=open("account.db","wb") f.write(bytes(json.dumps(account_dict),encoding="utf-8")) #将变更后的内容dumps入文件 f.close() print (account_dict) -----------------------------------------------------------------------------
{'1000': {'name': 'Tom', 'email': '22016311@qq.com', 'passwd': 'abc123', 'balance': 20000, 'phone': '44165465', 'bank_acc': {'ICBC': 165116161, 'CBC': 16516516516, 'ABC': 656151616516}}, '1001': {'name': 'Tom1', 'email': '220116311@qq.com', 'passwd': 'abc1123', 'balance': 210000, 'phone': '441654165', 'bank_acc': {'ICBC': 16511161611, 'CBC': 165165165161, 'ABC': 6561516165161}}} {'1000': {'name': 'Tom', 'email': '22016311@qq.com', 'passwd': 'abc123', 'balance': 19500, 'phone': '44165465', 'bank_acc': {'ICBC': 165116161, 'CBC': 16516516516, 'ABC': 656151616516}}, '1001': {'name': 'Tom1', 'email': '220116311@qq.com', 'passwd': 'abc1123', 'balance': 210000, 'phone': '441654165', 'bank_acc': {'ICBC': 16511161611, 'CBC': 165165165161, 'ABC': 6561516165161}}} {'1000': {'name': 'Tom', 'email': '22016311@qq.com', 'passwd': 'abc123', 'balance': 19500, 'phone': '44165465', 'bank_acc': {'ICBC': 165116161, 'CBC': 16516516516, 'ABC': 656151616516}}, '1001': {'name': 'Tom1', 'email': '220116311@qq.com', 'passwd': 'abc1123', 'balance': 210000, 'phone': '441654165', 'bank_acc': {'ICBC': 16511161611, 'CBC': 165165165161, 'ABC': 6561516165161}}}
|