day22-2018-11-15-模組2
阿新 • • 發佈:2018-11-16
import pickle # class Cat: # def __init__(self, name, color): # self.name = name # self.color = color # # def chi(self): # print("%s貓會吃老鼠" % self.name) # c = Cat("汪峰","黑色") # # bs = pickle.dumps(c) # 把一個物件轉化成bytes # print(bs) # cc = pickle.loads(b'\x80\x03c__main__\nCat\nq\x00)\x81q\x01}q\x02(X\x04\x00\x00\x00nameq\x03X\x06\x00\x00\x00\xe6\xb1\xaa\xe5\xb3\xb0q\x04X\x05\x00\x00\x00colorq\x05X\x06\x00\x00\x00\xe9\xbb\x91\xe8\x89\xb2q\x06ub.') # 把bytes轉化回物件# cc.chi() # print(cc.color, cc.name) # c1 = Cat("汪峰1","黑色") # c2 = Cat("汪峰2","黑色") # c3 = Cat("汪峰3","黑色") # # lst = [c1, c2, c3] # f = open("cat.dat", mode="ab") # pickle.dump(lst, f) # 把物件寫到檔案中 # # # # f = open("cat.dat", mode="rb") # lst = pickle.load(f) # 讀取第一次 # for cc in lst: # cc.chi() classUser: def __init__(self, username, password): self.username = username self.password = password class client: def regist(self): uname = input("please input your username:") pwd = input("please input your password:") user = User(uname, pwd) pickle.dump(user, open("userinfo", mode="ab")) print("regist successful!!!") def login(self): uname = input("please input your username:") pwd = input("please input your password:") f = open("userinfo", mode="rb") while 1: try: u = pickle.load(f) # 從檔案裡把物件拿出來 if u.username == uname and u.password == pwd: print("login successful !!") break except Exception as e: print("login failed !!!") break c = client() # c.regist() # c.regist() # c.regist() # c.regist() c.login()
import shelve # d = shelve.open("sylar") # 檔案型別的字典 # d['wf'] = "汪峰" # d.close() # # d = shelve.open("sylar") # print(d['wf']) # d.close() # d = shelve.open("sylar") # 檔案型別的字典 # d['wf'] = {"name":"汪峰", "age": 18, "wife":{"name":"章子怡", "hobby":"拍電影"}} # d.close() # d = shelve.open("sylar", writeback=True) # 檔案型別的字典 wirteback把修改的內容自動的回寫到檔案中 # d['wf']['wife']['hobby'] = "當導師" # 改 # d.close() # d = shelve.open("sylar") # 檔案型別的字典 # print(d['wf']) # d.close() d = shelve.open("sylar") for k, v in d.items(): print(k, v) print(type(d))
import configparser # config = configparser.ConfigParser() # 建立物件 # # config['DEFAULT'] = { # 特殊 # "name":"騰訊qq木馬", # "time":"qq更新時間", # "version":"1.0" # } # config['SERVER_1'] = { # "IP":"192.168.1.123", # "port":"12306" # } # config['SERVER_2'] = { # "IP":"192.168.1.178", # "port":"12311" # } # config['SERVER_3'] = { # "IP":"192.168.1.176", # "port":"12312" # } # # # 寫入到檔案 # config.write(open("qq.ini", mode="w", encoding="utf-8")) # 讀取內容 config = configparser.ConfigParser() # 讀取內容 config.read("qq.ini", encoding="utf-8") # 此時我們把檔案中的內容讀取到config print(config['SERVER_1']['IP']) # 字典 print(config['SERVER_2']['name']) print(config.get("SERVER_3", "IP")) # 字典 for k, v in config['DEFAULT'].items(): print(k, v) # config = configparser.ConfigParser() # # 讀取內容 # config.read("qq.ini", encoding="utf-8") # 此時我們把檔案中的內容讀取到config # config['SERVER_1']['NAME'] = "哈哈哈" # config.write(open("qq.ini", mode="w", encoding="utf-8"))
import json # # 準備一個字典 # dic = {"a": "小蘿莉", "b": "大蘿莉", "c": "猥瑣大叔", "d": False, "e": None} # # python中可以直接把字典或者列表轉化成json # s = json.dumps(dic, ensure_ascii=False) # pickle # print(type(s)) # print(s) # s = '{"a": "小蘿莉", "b": "大蘿莉", "c": "猥瑣大叔", "d": false, "e": null}' # d = json.loads(s) # 把json轉化成字典 # print(d) # print(type(d)) # dic = {"a": "小蘿莉", "b": "大蘿莉", "c": "猥瑣大叔", "d": False, "e": None, "wf":{"name":"半壁江山", "hobby":"皮褲"}} # f = open("sylar.json", mode="w", encoding="utf-8") # json.dump(dic, f, ensure_ascii=False, indent=4) # 4個空格 = 1個tab # # f = open("sylar.json", mode="r", encoding="utf-8") # d = json.load(f) # print(d) # class Person: # def __init__(self, firstName, lastName): # self.firstName = firstName # self.lastName = lastName # # s = '{"firstName": "尼古拉斯", "lastName": "劉能"}' # def func(dic): # return Person(dic['firstName'], dic["lastName"]) # # p = json.loads(s, object_hook=func) # 通過函式func把字典轉換回物件 # print(p.firstName, p.lastName) # p = Person("尼古拉斯", "劉能") # 把物件轉換成json # s = json.dumps(p.__dict__, ensure_ascii=False) # 方案一, 轉的是字典 # def func(obj): # return { # "firstName": obj.firstName, # "lastName": obj.lastName # } # s = json.dumps(p, default=func, ensure_ascii=False) # 方案二 轉化的也是字典 # print(s) dic1 = {"name":'毒液', "評分": "0.9"} dic2 = {"name":'與神同行', "評分": "10"} dic3 = {"name":'看不見的客人', "評分": "9.5"} # lst = [dic1, dic2, dic3] # f = open("movie.json", mode="w", encoding="utf-8") # for d in lst: # s = json.dumps(d, ensure_ascii=False) # f.write(s+"\n") # f = open("movie.json", mode="r", encoding="utf-8") # dic1 = json.load(f) # 當json檔案中儲存多個json的時候不能一次性全部都讀取出來 # print(dic1) f = open("movie.json", mode="r", encoding="utf-8") for line in f: line = line.strip() if line == "": continue else: d = json.loads(line) # 一行一行的處理 print(d)