一、字典格式数据源写入数据库文件
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 import pickle 4 accounts ={1000:'alex', 5 'name':'xxx', 6 'email':'xxxxsss@163.com', 7 'password':'abc123', 8 'balance':100000, 9 'phone':13643424246, 10 'bank_acc':{'icbc':132435,'abc':557868687,'bac':787977564}} 11 12 print(pickle.dumps(accounts)) 13 f=open('account.db','wb') 14 f.write(pickle.dumps(accounts)) 15 f.close()
二、打开文件,修改文件,关闭文件
1 import pickle 2 accFile = open('account.db','rb') 3 accDict = pickle.loads(accFile.read()) 4 accFile.close() 5 accDict['balance']-=500 6 7 8 f=open('account.db','wb') 9 f.write(pickle.dumps(accDict)) 10 f.close() 11 print(accDict)
三、打开文件最后核查
1 import pickle 2 f = open('account.db','rb') 3 account_db = pickle.loads(f.read()) 4 f.close() 5 print(account_db)