Python personal notes, purely convenient to query
In the middle of the exit to save, and then enter again when the point of exit. dictionary-----string (hard disk)------dictionary Pickle Syntax dump: write the dictionary to the hard disk file: import pickleaccount_info = {' a ': ' bbbbbbbbb ', ' b ': ' CCCCCCCCC '}f=file (' account.pki ', ' WB ') #定义一个二进制文件, Only strings can be written to the hard disk, so the dictionary is first converted to a string. Pickle.dump (account_info,f) F.close () reads a file from the hard disk into a dictionary: import picklepki_file=open (' account.pki ', ' RB ') Account_ List=pickle.load (pki_file) pki_file.close () If you do not write to a file, but instead send a message via the socket: a= {' a ': ' bbbbbbbbb ', ' b ': ' CCCCCCCCC '}b=pickle.dumps (a) c=pickle.loads (b) Pickle regular expression: import rep=re.compile (' Hello ') #匹配hellostr_a = ' Hello,my nam is darren ' p.match (str_a) m=p.match (str_a) m.group () #看返回值, If none then there is no match-----------------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+ matching spaces print re.findall (' \s+ ', str_a) #\s+ matches the Print re.findall (' \d+ ', str_a) that remove the space and #\d+ matches the print of the number re.findall (' \d+ ', str_a) #\d+ match the print re.split that removed the number (' \D+ ', str_a) #匹配到的做为分隔符print re.split (' b ', str_a) #以b做为分隔符. Print re.split (' \\\\ ', str_a) #前面两个为转义, followed by \ \ For two separators Print re.split ( Re.escape (' \ \ '), str_a) #和上面一样re. Sub (' \d+ ', ' | ', a) #把a中的空格替换为 |. Re.sub (' \d+ ', ' | ', a,1) #把a中的空格替换为 |, matching only one . #匹配任意一个字符 .+ #匹配任意一个或多个字符 -----------------------------python module--------------------------------if You import a directory such as day3, you will need to create an empty file in the Day3 directory. cd day3touch __init__.py Import an interactive configuration file, you need to first change the swap script to a function:def sayhi (): If dayhi ==main ()----------------------------------------python------------------------------------------adorner: View Sayhi () The time it took to execute:import Timedef sayhi (): start=time.time () print ' Hi,your sister time.sleep (0.5) end=time.time () print ' this function costs : ', end - startsayhi () If the time to calculate 100 functions takes: 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&Nbsp;sister ' @time_counterdef salary (): print ' your salary: ' Sayhi () salary ()
This article from "small East Elder brother" blog, declined reprint!
Section nineth: python pickle serialization, adorners, modules