標籤:python 二進位 import 運算式
python個人筆記,純屬方便查詢
中間退出的時候要儲存,然後再次進入的時候還是退出的那個點。字典-----字串(硬碟)------字典pickle文法dump:把字典寫入硬碟檔案中:import pickleaccount_info = {‘a‘:‘bbbbbbbbb‘,‘b‘:‘ccccccccc‘}f=file(‘account.pki‘,‘wb‘) #定義一個二進位檔案,只有字串才能寫入硬碟,所以字典要先轉換為字串。pickle.dump(account_info,f)f.close()從硬碟中讀取檔案變成字典:import picklepki_file=open(‘account.pki‘,‘rb‘)account_list=pickle.load(pki_file)pki_file.close() 如果不寫到檔案,而是通過socket發送訊息:a= {‘a‘:‘bbbbbbbbb‘,‘b‘:‘ccccccccc‘}b=pickle.dumps(a)c=pickle.loads(b)pickleRegex:import rep=re.compile(‘hello‘) #匹配hellostr_a= ‘hello,my nam is darren‘p.match(str_a)m=p.match(str_a)m.group() #看傳回值,如果是none則沒有匹配-----------------p=re.compile(‘my‘)p.serach(str_a) #匹配整行if m is not none:print "mactched"----------------m=re.search(‘my‘,str_a) m.group()re.search(‘my‘,str_a).group() #一行搞定模糊查詢:print re.findall(‘\s+‘, str_a) #\s+匹配空格的print re.findall(‘\S+‘, str_a) #\S+匹配去掉空格的print re.findall(‘\d+‘, str_a) #\d+匹配數位print re.findall(‘\D+‘, str_a) #\D+匹配去掉數位print re.split(‘\d+‘, str_a) #匹配到的做為分隔字元print re.split(‘b‘, str_a) #以b做為分隔字元。print re.split(‘\\\\‘, str_a) #前面兩個為轉義,後面兩個以\\為分隔字元print re.split(re.escape(‘\\‘), str_a) #和上面一樣re.sub(‘\d+‘,‘|‘,a) #把a中的空格替換為|。re.sub(‘\d+‘,‘|‘,a,1) #把a中的空格替換為|,只匹配一個. #匹配任意一個字元.+ #匹配任意一個或多個字元 -----------------------------python模組--------------------------------如果匯入一個目錄:例如day3,這時就需要在day3目錄下建一個空檔案。cd day3touch __init__.py匯入一個互動的設定檔,需要先把交換的指令碼改成一個函數:def sayhi ():if dayhi ==main()----------------------------------------python------------------------------------------裝飾器:查看sayhi()執行花費的時間:import timedef sayhi(): start=time.time() print ‘hi,your sister.....‘ time.sleep(0.5) end=time.time() print ‘this function costs :‘,end - startsayhi()如果計算一百個函數花費的時間:import timedef time_counter(func): def wrapper(): start=time.time() func() end=time.time() print ‘this function costs :‘,end - start return wrapper @time_counter def sayhi(): print ‘hi,your sister.....‘@time_counterdef salary(): print ‘your salary:‘sayhi() salary()
本文出自 “小東哥” 部落格,謝絕轉載!
第九節:python pickle序列化、裝飾器、模組