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