day_24(序列化模塊)
阿新 • • 發佈:2018-07-06
序列 如果 ascii 引號 .org use 問題 alex error:
# 模塊: 一個py文件就是一個模塊. ‘‘‘ python開發效率之高:Python的模塊非常多,第三方庫. 模塊分類: 1,內置模塊:登錄模塊,時間模塊,sys模塊,os模塊 等等. 2,擴展模塊. itchat 微信有關.爬蟲: beautifulsoup 所有的擴展模塊:https://pypi.org/ 3,自定義模塊.自己寫的py文件. ‘‘‘ # 序列化模塊. #序列化:創造一個序列. #實例化:創造一個實例(對象). # 將一個字典通過網絡傳輸給另一個人. ‘‘‘ 文件中可以存儲:字符串,和bytes. 數據的傳輸:bytes類型.‘‘‘
# 序列化: 創造一個序列, ---> 特殊處理(序列化的)字符串. #序列化: # json: # 適用於不同語言之間的, # 但是可支持的數據類型:字符串,數字,列表,字典 bool待定。 # pickle: # 只用於python語言之間的. #可支持python所有的數據類型. # shelve(了解):只是python,小工具(文件方面). #序列化過程: 一個數據類型 ---> 序列化的字符串#反序列化過程: 序列化的字符串 ---> 它所對應的數據類型
# dumps loads 網絡的傳輸 # dic = {"alex": [‘women‘,‘women‘,‘老女人‘],‘p1‘:True} # dic = {"alex": (‘women‘,‘women‘,‘老女人‘)} # print(str(dic)) # 基礎數據類型str 裏面如果有引號就是單引號 # ret = json.dumps(dic,ensure_ascii=False) # 序列化過程:數據類型dic---> 序列化的字符串 # print(ret,type(ret)) # 被json序列化的字符串:#1,可以直接通過網絡互相傳輸. #2,可以在各個語言中通用. # dic1 = json.loads(ret) # 反序列化過程.:將序列化的字符串---> 原有的數據類型. # print(dic1,type(dic1)) #dump load 有關文件存儲 # import json # l1 = [‘張三‘,‘歷史‘,‘王五‘,‘alex‘,‘老土‘,‘旭哥‘] # f = open(‘json_file‘,encoding=‘utf-8‘,mode=‘w‘) # json.dump(l1,f,ensure_ascii=False) # 將序列化的字符串存儲到文件中 # f.close() # f = open(‘json_file‘,encoding=‘utf-8‘) # ret = json.load(f) # print(ret,type(ret)) # f.close()
# 有關文件存儲的問題? # dic = {"alex": (‘women‘,‘women‘,‘老女人‘)} # dic2 = {"alex1": (‘women‘,‘women‘,‘老女人‘)} # dic3 = {"alex2": (‘women‘,‘women‘,‘老女人‘)} # f = open(‘json_files‘,encoding=‘utf-8‘,mode=‘w‘) # json.dump(dic,f,ensure_ascii=False) # json.dump(dic2,f,ensure_ascii=False) # json.dump(dic3,f,ensure_ascii=False) # f.close() # f = open(‘json_files‘, encoding=‘utf-8‘,) # print(json.load(f)) # print(json.load(f)) # print(json.load(f)) # f.close()
# 將多個序列化的字符串寫入文件,然後反序列化,就會出錯 # 用 dump load 只能寫入和讀取文件 一個序列化的字符串
# 用dumps和loads操作
# dic = {"alex": (‘women‘,‘women‘,‘老女人‘)} # dic2 = {"alex1": (‘women‘,‘women‘,‘老女人‘)} # dic3 = {"alex2": (‘women‘,‘women‘,‘老女人‘)} # with open(‘json_files‘,encoding=‘utf-8‘,mode=‘a‘) as f1: # s1 = json.dumps(dic,ensure_ascii=False) # f1.write(s1+‘\n‘) # s2 = json.dumps(dic2,ensure_ascii=False) # f1.write(s2+‘\n‘) # s3 = json.dumps(dic3,ensure_ascii=False) # f1.write(s3+‘\n‘) # # with open(‘json_files‘,encoding=‘utf-8‘) as f2: # for line in f2: # dic = json.loads(line) # print(dic,type(dic))
# 其他參數 # import json # data = {‘username‘:[‘李華‘,‘二楞子‘],‘sex‘:‘male‘,‘age‘:16,‘A‘:666} # json_dic2 = json.dumps(data,sort_keys=True,indent=2,separators=(‘|‘,‘*‘),ensure_ascii=False) # print(json_dic2) # # print(json.loads(json_dic2)) # 如果改了:separators=(‘|‘,‘*‘)反序列化不行了 # sort_keys=True 字典鍵的首字母的ascii碼排序 # ensure_ascii=False 顯示中文 # indent=2 key 縮進 # dic = {(1,2,3):‘alex‘,1:[1,2,3]} # ret = json.dumps(dic) # print(ret) # TypeError: keys must be a string
# dumps loads 網絡傳輸 # dic = {1:True,(2,3):[1,2,3,4],False:{1,2,3}} # import pickle # ret = pickle.dumps(dic) # bytes類型無法識別內容 # # dic1 = pickle.loads(ret) # print(dic1,type(dic1)) # dump load 文件操作 # dic = {1:True,(2,3):[1,2,3,4],False:{1,2,3}} # import pickle # with open(‘pickle_file‘,mode=‘wb‘) as f1: # pickle.dump(dic,f1) # with open(‘pickle_file‘,mode=‘rb‘) as f2: # print(pickle.load(f2))
# 多個數據存儲到一個文件 (dump.load) # dic = {"alex": (‘women‘,‘women‘,‘老女人‘)} # dic2 = {"alex1": (‘women‘,‘women‘,‘老女人‘)} # dic3 = {"alex2": (‘women‘,‘women‘,‘老女人‘)} # import pickle # with open(‘pickle_files‘,mode=‘wb‘) as f1: # pickle.dump(dic,f1) # pickle.dump(dic2,f1) # pickle.dump(dic3,f1) # pickle.dump(dic3,f1) # with open(‘pickle_files‘,mode=‘rb‘) as f1: # while True: # try: # print(pickle.load(f1)) # except EOFError: # break
shelve 與文件相關
# import shelve # f = shelve.open(‘shelve_file‘) # f[‘key‘] = {‘int‘:10, ‘float‘:9.5, ‘string‘:‘Sample data‘} #直接對文件句柄操作,就可以存入數據 # f.close() # import shelve # f1 = shelve.open(‘shelve_file‘) # existing = f1[‘key‘] #取出數據的時候也只需要直接用key獲取即可,但是如果key不存在會報錯 # f1.close() # print(existing)
day_24(序列化模塊)