标签:
1.如果没有使用模块,那么保存一个字典只能以字符串的形式保存到文件中去
info = { "alex":"124", "Tony":"456" } f = open("user_info.txt",‘w‘) f.write(str(info))
2.使用pickle模块来序列化数据
import pickle info = { "alex":"124", "Tony":"456" } f = open("user_info.txt",‘wb‘) f.write(pickle.dumps(info)) import pickle f = open(‘user_info.txt‘,‘rb‘) data_from_atm = pickle.loads(f.read()) print (data_from_atm)
3.load/dump以及loads/dumps方法的区别
import pickle info = { "alex":"124", "Tony":"456" } f = open("user_info.txt",‘wb‘) pickle.dump(info,f) import pickle f = open(‘user_info.txt‘,‘rb‘) data_from_atm = pickle.load(f) print (data_from_atm)
4.使用json模块
import json info = { "alex":"124", "Tony":"456" } f = open("user_info.txt",‘w‘) f.write(json.dumps(info)) import json f = open(‘user_info.txt‘,‘r‘) data_from_atm = json.loads(f.read()) print (data_from_atm)
标签:
原文地址:http://www.cnblogs.com/python-study/p/5513741.html