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['] [' balance '])) account_dict[' + ' [' balance ']-= #取字典中balance然后减500 P Rint (account_dict) F=open ("Account.db", "WB") F.write (bytes (json.dumps), account_dict "encoding=" )) #将变更后的内容dumps入文件 F.close () Print (account_dict) ---------------------------------------------------- -------------------------
{' + ': {' name ': ' Tom ', ' email ': ' [email protected] ', ' passwd ': ' abc123 ', ' balance ': 20000, ' phone ': ' 44165465 ', ' Bank_ac C ': {' ICBC ': 165116161, ' CBC ': 16516516516, ' ABC ': 656151616516}}, ' 1001 ': {' name ': ' Tom1 ', ' email ': ' [email protected] ', ' passwd ': ' abc1123 ', ' balance ': 210000, ' phone ': ' 441654165 ', ' Bank_acc ': {' ICBC ': 16511161611, ' CBC ': 165165165161, ' ABC ': 6561516165161}}} {' + ': {' name ': ' Tom ', ' email ': ' [email protected] ', ' passwd ': ' abc123 ', ' balance ': 19500, ' phone ': ' 44165465 ', ' Bank_ac C ': {' ICBC ': 165116161, ' CBC ': 16516516516, ' ABC ': 656151616516}}, ' 1001 ': {' name ': ' Tom1 ', ' email ': ' [email protected] ', ' passwd ': ' abc1123 ', ' balance ': 210000, ' phone ': ' 441654165 ', ' Bank_acc ': {' ICBC ': 16511161611, ' CBC ': 165165165161, ' ABC ': 6561516165161}}} {' + ': {' name ': ' Tom ', ' email ': ' [email protected] ', ' passwd ': ' abc123 ', ' balance ': 19500, ' phone ': ' 44165465 ', ' Bank_ac C ': {' ICBC ': 165116161, ' CBC ': 16516516516, ' ABC ': 656151616516}}, ' 1001 ': {' name ': ' Tom1 ', ' email ': ' [email protected] ', ' passwd ': ' abc1123 ', ' balance ': 210000, ' phone ': ' 441654165 ', ' Bank_acc ': {' ICBC ': 16511161611, ' CBC ': 165165165161, ' ABC ': 6561516165161}}} |