序列化 把对象打散成二进制字节 bytes
一. pickle 把一个对象转化成bytes写入到文件 (重点)
pickle.dumps() 把对象转化成bytes
pickle.loads() 把bytes转化成对象
pickle.dump() 把对象转化成bytes, 写入到文件
pickle.load() 把文件中的bytes读取, 转化成对象
1 import pickle 2 # class hui(): 3 # def __init__(self,name,age): 4 # self.name = name 5 # self.age = age 6 # def hello(self): 7 # print('Hello World!') 8 # h = hui('hui',18) 9 10 # bs = pickle.dumps(h) #序列化一个对象 11 # print(bs,type(bs))#一堆二进制,看不懂 12 13 # hh = pickle.loads(bs) #把二进制反序列化成我们的对象 14 # print(hh,type(hh)) #对象中的成员 15 # print(hh.name) 16 17 # f = open('hui_pickle',mode='wb',) 18 # pickle.dump(h,f) #写入到文件中 19 20 # hh = pickle.load(open('hui_pickle',mode='rb')) #从文件中读取对象 21 # hh.hello() 22 23 # LST = [hui('A',1),hui('B',2),hui('C',3)] 24 # pickle.dump(LST,open('hui_pickle',mode='wb')) #写入到文件中 25 26 # f = open('hui_pickle',mode='rb') 27 # ls = pickle.load(f) #从文件中读取对象 28 # for i in ls: 29 # i.hello()
二. shelve 小型数据库, redis,mongodb,dict
当成字典来用
writeback = True
1 import shelve 2 3 # d = shelve.open("sylar") # 文件类型的字典 4 # d['wf'] = "汪峰" 5 # d.close() 6 # 7 # d = shelve.open("sylar") 8 # print(d['wf']) 9 # d.close() 10 11 # d = shelve.open("sylar") # 文件类型的字典 12 # d['wf'] = {"name":"汪峰", "age": 18, "wife":{"name":"章子怡", "hobby":"拍电影"}} 13 # d.close() 14 15 # d = shelve.open("sylar", writeback=True) # 文件类型的字典 wirteback把修改的内容自动的回写到文件中 16 # d['wf']['wife']['hobby'] = "当导师" # 改 17 # d.close() 18 19 # d = shelve.open("sylar") # 文件类型的字典 20 # print(d['wf']) 21 # d.close() 22 23 24 d = shelve.open("sylar") 25 for k, v in d.items(): 26 print(k, v) 27 print(type(d))
三. json 以前用xml 现在用json (重点)
json.dumps() 把字典转化成json字符串
json.loads() 把json字符串转化成字典
json.dump() 把字典转化成json字符串写入到文件
json.load() 把文件中的json字符串读取, 转化成字典
default = 把对象转化成字典, 需要自己写转化过程
object_hook = 把字典转化成对象. 需要自己写转化过程
ensure_ascii = False 可以处理中文
1 # json 2 import json 3 # dic = {'A':1,'大写':2} 4 # s = json.dumps(dic,ensure_ascii=False) #把字典转化成json字符串 5 # print(s,type(s)) 6 7 # ss = '{"A": 1, "大写": 2}' 8 # d = json.loads(ss) 9 # print(d,type(d)) 10 11 # json.dump(dic,open('hui_json',mode='w')) #把对象打散成json写入到文件中 12 # d = json.load(open('hui_json',mode='r')) #读取 13 # print(d,type(d)) 14 15 # lst = [{'尼1':1},{'尼2':2},{'尼3':3}] 16 # json.dump(lst,open('hui_json',mode='w',encoding='utf-8'),ensure_ascii=False) 17 # l = json.load(open('hui_json',mode='r',encoding='utf-8')) 18 # for i in l: 19 # print(i,type(i))
四. configparser 处理Windows 配置文件的 dict
1 import configparser 2 3 # config = configparser.ConfigParser() # 创建对象 4 # 5 # config['DEFAULT'] = { # 特殊 6 # "name":"腾讯qq木马", 7 # "time":"qq更新时间", 8 # "version":"1.0" 9 # } 10 # config['SERVER_1'] = { 11 # "IP":"192.168.1.123", 12 # "port":"12306" 13 # } 14 # config['SERVER_2'] = { 15 # "IP":"192.168.1.178", 16 # "port":"12311" 17 # } 18 # config['SERVER_3'] = { 19 # "IP":"192.168.1.176", 20 # "port":"12312" 21 # } 22 # 23 # # 写入到文件 24 # config.write(open("qq.ini", mode="w", encoding="utf-8")) 25 26 27 # 读取内容 28 config = configparser.ConfigParser() 29 # 读取内容 30 config.read("qq.ini", encoding="utf-8") # 此时我们把文件中的内容读取到config 31 print(config['SERVER_1']['IP']) # 字典 32 print(config['SERVER_2']['name']) 33 print(config.get("SERVER_3", "IP")) # 字典 34 35 for k, v in config['DEFAULT'].items(): 36 print(k, v) 37 38 # config = configparser.ConfigParser() 39 # # 读取内容 40 # config.read("qq.ini", encoding="utf-8") # 此时我们把文件中的内容读取到config 41 # config['SERVER_1']['NAME'] = "哈哈哈" 42 # config.write(open("qq.ini", mode="w", encoding="utf-8"))