1 import pickle 2 3 4 class User: 5 def __init__(self,username,password): 6 self.username = username 7 self.password = password 8 9 10 class Client: 1 ...
# 序列化:存儲或傳輸數據時,把對象處理成方便存儲和傳輸的數據格式,這個過程即為序列化
# Python中序列化的三種方案:
# 1.pickle python任意數據——》bytes寫入⽂件;寫好的bytes——》python的數據.
# 2.shelve 簡單另類的⼀種序列化⽅案. 可以作為⼀種⼩型的資料庫來使⽤
# 3.json 將字典列表轉換成字元串,前後端數據交互高頻使用的⼀種數據格式
# pickle:
# 寫入到文件的是bytes
# pickle中的dumps可以序列化⼀個對象.loads可以反序列化⼀個對象
1 import pickle 2 class Cat: 3 def __init__(self,name,color): 4 self.name = name 5 self.color =color 6 7 def chi(self): 8 print("%s貓會吃老鼠"%(self.name)) 9 10 c = Cat("加菲","橘色") 11 # c.chi() 12 bs = pickle.dumps(c) # 把對象轉換成bytes 13 # print(bs) # b'\x80\x03c__main__\nCat\nq\x00)\x81q\x01}q\x02(X\x04\x00\x00\x00nameq\x03X\x06\x00\x00\x00\xe5\x8a\xa0\xe8\x8f\xb2q\x04X\x05\x00\x00\x00colorq\x05X\x06\x00\x00\x00\xe6\xa9\x98\xe8\x89\xb2q\x06ub.' 14 15 # 把bytes反序列化成對象 16 cc = pickle.loads(b'\x80\x03c__main__\nCat\nq\x00)\x81q\x01}q\x02(X\x04\x00\x00\x00nameq\x03X\x06\x00\x00\x00\xe5\x8a\xa0\xe8\x8f\xb2q\x04X\x05\x00\x00\x00colorq\x05X\x06\x00\x00\x00\xe6\xa9\x98\xe8\x89\xb2q\x06ub.') 17 cc.chi() 18 print(cc.name,cc.color) # 加菲貓會吃老鼠 加菲 橘色
# dump load讀寫文件操作
# dump load ⼀個對象寫讀到文件
1 c = Cat("加菲","橘色") 2 pickle.dump(c,open("cat.dat",mode = 'wb')) # 把對象寫到文件 pickle.dump() 註意bytes用wb模式 3 ccc = pickle.load(open('cat.dat',mode='rb')) # 把對象讀取出來 pickle.load() 註意bytes用rb模式 4 ccc.chi() # 加菲貓會吃老鼠 5 print(ccc.name,ccc.color) # 加菲 橘色
# pickle用list讀寫多個對象
1 c1 = Cat("加菲1","橘色") 2 c2 = Cat("加菲2","橘色") 3 c3 = Cat("加菲3","橘色") 4 c4 = Cat("加菲4","橘色") 5 c5 = Cat("加菲5","橘色") 6 7 # lst = [c1,c2,c3,c4,c5] 8 # f = open('cat.dat',mode='wb') 9 # pickle.dump(lst,f) # 把lst寫到f 10 11 f = open('cat.dat',mode='rb') 12 lis = pickle.load(f) # 把lis從f讀取出來 13 for cc in lis: 14 cc.chi()
# 應用:pickle實現註冊登錄
1 import pickle 2 3 4 class User: 5 def __init__(self,username,password): 6 self.username = username 7 self.password = password 8 9 10 class Client: 11 # 註冊 12 def regist(self): 13 uname = input("請輸入你的賬戶:") 14 pwd = input("請輸入你的密碼:") 15 user = User(uname,pwd) # 把uname pwd 傳參到User類 16 pickle.dump(user,open("userinfo",mode='ab')) # 把對象保存到userinfo 17 print("註冊成功!") 18 19 # 登錄 20 def login(self): 21 uname = input("請輸入你的賬戶:") 22 pwd = input("請輸入你的密碼:") 23 f = open('userinfo',mode='rb') 24 while 1: 25 # 加入try except 當不存在的賬戶登錄時 拋出錯誤 26 try: 27 u = pickle.load(f) # 從userinfo中讀取出用戶 28 if u.username == uname and u.password == pwd: 29 print("登陸成功!") 30 break 31 except Exception as e: 32 print("登錄失敗!") 33 break 34 35 d = Client() 36 # print("註冊") 37 # d.regist() 38 # d.regist() 39 # d.regist() 40 # print("登錄") 41 # d.login() 42 # d.login() 43 # d.login()View Code
# shelve 就是把數據寫到硬碟上.操作shelve非常的像操作字典.
1 import shelve 2 3 d = shelve.open("test") # 可理解成文件類型的字典 4 # d['wf'] = '汪峰' 5 d['wf'] = '王菲' 6 print(d['wf']) # 汪峰 王菲 7 d.close()
# 存儲一些複雜點的數據
1 import shelve 2 d = shelve.open('test') 3 d['wf'] = {'name':'汪峰','age':47,'wife':{'name':'章子怡','hobby':'拍電影'}} 4 print(d['wf']) # 運行結果 {'name': '汪峰', 'age': 47, 'wife': {'name': '章子怡', 'hobby': '拍電影'}} 5 d.close() 6 7 # 嘗試修改 8 d = shelve.open('test') 9 d['wf']['wife']['name'] = '小章' 10 print(d['wf']) # 運行結果 {'name': '汪峰', 'age': 47, 'wife': {'name': '章子怡', 'hobby': '拍電影'}} 11 d.close() 12 # 坑 由上可知 直接修改沒效果 需用到 writeback
# writeback=True 可動態的把修改的信息寫入到⽂件中.
1 d = shelve.open('test',writeback=True) # 文件類型字典修改時 writeback 把修改回寫到文件 2 d['wf']['wife']['hobby'] = '搓麻將' # 修改vlue 3 d.close() 4 5 d = shelve.open('test') 6 print(d['wf']) # 運行結果 {'name': '汪峰', 'age': 47, 'wife': {'name': '章子怡', 'hobby': '搓麻將'}} 7 d.close()
# json 前後端交互的紐帶
# json全稱javascript object notation.翻譯js對象簡譜.
# json代碼(python中的字典)
1 wf = { 2 "name":"汪峰", 3 "age":18, 4 "hobby":"上頭條", 5 "wife":{ 6 "name":'⼦怡', 7 "age":19, 8 "hobby":["唱歌", "跳舞", "演戲"] 9 } 10 }
# 直接把字典轉化成json字元串
import json
1 dic = {'a':'一隻蘿莉','b':'兩隻蘿莉','c':'一群蘿莉','d':False,'e':None} 2 s = json.dumps(dic,ensure_ascii=False) # ensure_ascii 固定套路不轉成bytes 3 print(type(s)) # <class 'str'> 4 print(s) # {"a": "一隻蘿莉", "b": "兩隻蘿莉", "c": "一群蘿莉", "d": false, "e": null}
# 把json字元串轉換成字典
1 s1 = '{"a": "一隻蘿莉", "b": "兩隻蘿莉", "c": "一群蘿莉", "d": false, "e": null}' 2 d = json.loads(s1) 3 print(d) # {'a': '一隻蘿莉', 'b': '兩隻蘿莉', 'c': '一群蘿莉', 'd': False, 'e': None} 4 print(type(d)) # <class 'dict'>
# 把json寫入文件
1 dic = {'a':'小蘿莉','b':'大蘿莉','c':'一群蘿莉','d':False,'e':None,'wf':{'name':'怒放的生命','hobby':'皮褲'}} 2 f = open('loli.json',mode='w',encoding='utf-8') # 註意:utf-8格式寫入 3 json.dump(dic,f,ensure_ascii=False,indent=4) # indent=4 縮進4格 等同tab 便於閱讀
# 文件中讀取json
1 f = open('loli.json',mode='r',encoding="utf-8") 2 d = json.load(f) 3 # print(d) 4 f.close()
# 把對象轉換成json
1 class Person: 2 def __init__(self,firstName,lastName): 3 self.firstName = firstName 4 self.lastName = lastName 5 6 p = Person('尼古拉斯','趙四') 7 8 # 方案一 轉化的是字典 9 s = json.dumps(p.__dict__,ensure_ascii=False) 10 print(s) # {"firstName": "尼古拉斯", "lastName": "趙四"} 11 12 # 方案二 自己定義函數 13 def func(obj): 14 return { 15 'firstName':obj.firstName, 16 'lastName':obj.lastName 17 } 18 19 s = json.dumps(p,default=func,ensure_ascii=False) 20 print(s) # {"firstName": "尼古拉斯", "lastName": "趙四"}View Code
# 函數把字典(json)轉換成對象
1 s = '{"firstName": "尼古拉斯", "lastName": "趙四"}' 2 def func(dic): 3 return Person(dic['firstName'],dic['lastName']) 4 5 p = json.loads(s,object_hook=func) # 字典轉換成對象 6 7 print(p.firstName,p.lastName)
# 註意.我們可以向同⼀個⽂件中寫⼊多個json串.但是讀不⾏.
1 import json 2 lst = [{'a':1},{'b':2},{'c':3}] 3 f = open('test.json',mode='w',encoding='utf-8') 4 for el in lst: 5 json.dump(el,f) 6 f.close() 7 # 若需要讀取改用 dumps和loads 迴圈對每行分別處理 8 import json 9 lst = [{"a": 1}, {"b": 2}, {"c": 3}] 10 # 寫⼊ 11 f = open("test.json", mode="w", encoding="utf-8") 12 for el in lst: 13 s = json.dumps(el, ensure_ascii=True) + "\n" # 註意dumps這裡ensure_ascii是True 14 f.write(s) 15 f.close() 16 # 讀取 17 f = open("test.json", mode="r", encoding="utf-8") 18 for line in f: 19 dic = json.loads(line.strip()) 20 print(dic) 21 f.close()View Code
# configparser模塊(參考)
# 模塊用於配置⽂件格式與windows ini⽂件類似,可以包含⼀個或多個節(section)每個節可有多個參數(鍵=值)
# 伺服器配置文件
1 ''' 2 [DEFAULT] [DEFAULT] 3 ServerAliveInterval = 45 4 Compression = yes 5 CompressionLevel = 9 6 ForwardX11 = yes 7 [[bitbucket.org bitbucket.org]] 8 User = hg 9 [[topsecret.server.com topsecret.server.com]] 10 Port = 50022 11 ForwardX11 = no 12 '''
# ⽤configparser對這樣的⽂件進⾏處理
# 初始化
1 import configparser 2 3 config = configparser.ConfigParser() 4 config['DEFAULT'] = { 5 "sleep": 1000, 6 "session-time-out": 30, 7 "user-alive": 999999 8 } 9 config['TEST-DB'] = { 10 "db_ip": "192.168.17.189", 11 "port": "3306", 12 "u_name": "root", 13 "u_pwd": "123456" 14 } 15 config['168-DB'] = { 16 "db_ip": "152.163.18.168", 17 "port": "3306", 18 "u_name": "root", 19 "u_pwd": "123456" 20 } 21 config['173-DB'] = { 22 "db_ip": "152.163.18.173", 23 "port": "3306", 24 "u_name": "root", 25 "u_pwd": "123456" 26 } 27 f = open("db.ini", mode="w") 28 config.write(f) # 寫⼊⽂件 29 f.flush() 30 f.close()View Code
# 讀取文件信息
1 config = configparser.ConfigParser() 2 config.read("db.ini") # 讀取⽂件 3 print(config.sections()) # 獲取到section. 章節...DEFAULT是給每個章節都配備的信息 4 print(config.get("DEFAULT", "SESSION-TIME-OUT")) # 從xxx章節中讀取到xxx信息 5 # 也可以像字典⼀樣操作 6 print(config["TEST-DB"]['DB_IP']) 7 print(config["173-DB"]["db_ip"]) 8 for k in config['168-DB']: 9 print(k) 10 for k, v in config["168-DB"].items(): 11 print(k, v) 12 print(config.options('168-DB')) # 同for迴圈,找到'168-DB'下所有鍵 13 print(config.items('168-DB')) #找到'168-DB'下所有鍵值對 14 print(config.get('168-DB','db_ip')) # 152.163.18.168 get⽅法Section下的key對應的valueView Code
# 增刪改操作
# 先讀取. 然後修改. 最後寫回⽂件
1 config = configparser.ConfigParser() 2 config.read("db.ini") # 讀取⽂件 3 # 添加⼀個章節 4 # config.add_section("189-DB") 5 # config["189-DB"] = { 6 # "db_ip": "167.76.22.189", 7 # "port": "3306", 8 # "u_name": "root", 9 # "u_pwd": "123456" 10 # } 11 # 修改信息 12 config.set("168-DB", "db_ip", "10.10.10.168") 13 # 刪除章節 14 config.remove_section("173-DB") 15 # 刪除元素信息 16 config.remove_option("168-DB", "u_name") 17 # 寫回⽂件 18 config.write(open("db.ini", mode="w"))View Code