1.pickle序列介绍:提供4个关键字:dumps,dump,loads,load
语法:f.write(pickle.dumps(dict))=pickle.dump(dict,f)
"r=pickle.loads(f.read())" = "r=pickle.load(f)
将字典存入文件,由于字典格式不能转化成字节格式的,所以引入pickle
A.将一个字典转化成pickle序列存入一个文件
import pickle f.close() |
B.重新创建一个新的python文件读取account.db中的内容:
import pickle account_file=open("account.db","rb") #打开文件 {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}}} |