1. 程式人生 > >假期(模塊相關)

假期(模塊相關)

完全 clas wds 讀取 驗證碼 取反 utf str return

# ----------------------------------------------------------------------------------------
import time
timestamp = time.time()    #時間戳
struct_time = time.localtime()      #結構化時間
format_time = time.strftime("%Y-%m-%d %X")   #格式化時間

# print(time.localtime(timestamp))         #  時間戳   ====== >  結構化時間
# print(time.mktime(struct_time)) # 結構化時間 ======> 時間戳 # print(time.strftime("%Y-%m-%d %X"),struct_time) #格式化時間 ===== > 結構化時間 # print(time.strptime("2018-02-13 09:25:45","%Y-%m-%d %X")) #結構化時間 === > 格式化時間 # print(time.asctime()) #Tue Feb 13 09:28:49 2018 # print(time.ctime(timestamp)) #Tue Feb 13 09:29:29 2018
# time.sleep(10) #睡多少秒 # ---------------------------------------------------------------------------------------- import random # print(random.random()) #0-1的隨機小數 # print(random.randint(1,4)) # 產生大於等於1,小於等於4的整數 # print(random.randrange(1,4)) # 產生大於等於1,小於4的整數 # print(random.choice([1,"23",[4,5]])) #隨機產生列表中的一項
# print(random.sample([1,"23",[4,5],2],2)) #第一個參數是列表,第二個參數是隨便產生的列表項 # print(random.uniform(1,3)) #產生1-3之間的隨機小數 # item = [1,2,3,4,5,6] # random.shuffle(item) #打亂原列表的順序,沒有返回值 # print(item) # 生成隨機驗證碼 # def make_code(n): # res = "" # for i in range(n): # s1 = chr(random.randint(65,90)) # s2 = str(random.randint(0,9)) # res+=random.choice([s1,s2]) # return res # print(make_code(9)) # ---------------------------------------------------------------------------------------- import os # print(os.getcwd()) #獲取當前工作目錄 # print(os.chdir("dirname")) #相當於cd切換命令 # os.remove("dirname") #刪除dirname目錄 # os.rename("oldname","newname") #重命名 # print(os.environ) #獲取當前的環境變量 # os.path.join("dirname") #路徑拼接 # ---------------------------------------------------------------------------------------- import sys # print(sys.argv) #當前程序的路徑 # print(sys.exit(0)) #退出程序 # print(sys.version) #獲取python的版、本信息 # print(sys.maxsize) #獲取最大int值 # print(sys.path) #返回模塊的搜索路徑 # print(sys.platform) #返回操作系統的名稱 # 實現打印進度條 # def progress(percent,width=50): # if percent >= 1: # percent = 1 # show_str = (‘[%%-%ds]‘%width)%(int(width*percent)*"#") # print(‘\r%s %d%%‘%(show_str,int(100*percent)),file=sys.stdout,flush=True,end="") # data_size = 1025 # recv_size = 0 # while recv_size < data_size: # time.sleep(0.1) # recv_size += 1024 # percent = recv_size/data_size # progress(percent,width=70) # ---------------------------------------------------------------------------------------- import shutil # shutil.copyfile("file1","file2") #將文件內容拷貝到另一個文中 # shutil.copymode("file1.log","file2.log") #僅僅拷貝權限 file2.log 必須存在 # shutil.copystat("file1","file2") #拷貝狀態信息,file2必須存在 # shutil.copy("file1.log","file2.log") #拷貝文件和權限 # shutil.copy2("file1.log","file2.log") #拷貝文件和狀態信息 # shutil.copytree() #遞歸拷貝 # shutil.rmtree("filepath") #遞歸刪除文件 # shutil.move("file1","file2") #遞歸的去移動文件 # ---------------------------------------------------------------------------------------- import json # x = "[1,2,3,4,5,6,7,8]" # print(x,type(x)) #[1,2,3,4,5,6,7,8] <class ‘str‘> # print(eval(x),type(eval(x))) #[1, 2, 3, 4, 5, 6, 7, 8] <class ‘list‘> # print(json.loads(x),type(json.loads(x))) #[1, 2, 3, 4, 5, 6, 7, 8] <class ‘list‘> # json數據主要用於跨平臺數據交互 # dic = {‘name‘:‘alvin‘,‘age‘:23,‘sex‘:‘male‘} # print(type(dic)) #<class ‘dict‘> # j = json.dumps(dic) # print(type(j)) # <class ‘str‘> # f = open("序列化對象","w") # f.write(j) # f.close() # #反序列化操作 # f = open("序列化對象","r") # data = json.loads(f.read()) # json不認單引號,json可以跨平臺交互,pickle只可以在python中數據交互 # import pickle # dic = {‘name‘:‘alvin‘,‘age‘:23,‘sex‘:‘male‘} # j = pickle.dumps(dic) # print(type(j)) #<class ‘bytes‘> # ---------------------------------------------------------------------------------------- import shelve # shelve模塊比pickle模塊更加的簡單,只有一個open函數,返回類字典對象,可讀可寫,key必須為字符串,值可以是python所支持的數據類型 # f=shelve.open(r‘sheve.txt‘) # # f[‘stu1_info‘]={‘name‘:‘egon‘,‘age‘:18,‘hobby‘:[‘piao‘,‘smoking‘,‘drinking‘]} # # f[‘stu2_info‘]={‘name‘:‘gangdan‘,‘age‘:53} # # f[‘school_info‘]={‘website‘:‘http://www.pypy.org‘,‘city‘:‘beijing‘} # # print(f[‘stu1_info‘][‘hobby‘]) # f.close() # ---------------------------------------------------------------------------------------- import xml #這個模塊以及過時,已經被lxml取代 # ---------------------------------------------------------------------------------------- import configparser # ---------------------------------------------------------------------------------------- import hashlib m = hashlib.md5() m.update("hello_zhang".encode("utf8")) print(m.hexdigest()) #ed5e024cfdceba3e24b4333709d2dc1a #模擬撞庫破解密碼 passwds=[ alex3714, alex1313, alex94139413, alex123456, 123456alex, a123lex, ] def make_passwd_dic(passwds): dic={} for passwd in passwds: m=hashlib.md5() m.update(passwd.encode(utf-8)) dic[passwd]=m.hexdigest() return dic def break_code(cryptograph,passwd_dic): for k,v in passwd_dic.items(): if v == cryptograph: print(密碼是===>\033[46m%s\033[0m %k) cryptograph=aee949757a2e698417463d47acac93df break_code(cryptograph,make_passwd_dic(passwds)) # ---------------------------------------------------------------------------------------- import re # =================================匹配模式================================= #一對一的匹配 # ‘hello‘.replace(old,new) # ‘hello‘.find(‘pattern‘) #正則匹配 import re #\w與\W print(re.findall(\w,hello egon 123)) #[‘h‘, ‘e‘, ‘l‘, ‘l‘, ‘o‘, ‘e‘, ‘g‘, ‘o‘, ‘n‘, ‘1‘, ‘2‘, ‘3‘] print(re.findall(\W,hello egon 123)) #[‘ ‘, ‘ ‘] #\s與\S print(re.findall(\s,hello egon 123)) #[‘ ‘, ‘ ‘, ‘ ‘, ‘ ‘] print(re.findall(\S,hello egon 123)) #[‘h‘, ‘e‘, ‘l‘, ‘l‘, ‘o‘, ‘e‘, ‘g‘, ‘o‘, ‘n‘, ‘1‘, ‘2‘, ‘3‘] #\n \t都是空,都可以被\s匹配 print(re.findall(\s,hello \n egon \t 123)) #[‘ ‘, ‘\n‘, ‘ ‘, ‘ ‘, ‘\t‘, ‘ ‘] #\n與\t print(re.findall(r\n,hello egon \n123)) #[‘\n‘] print(re.findall(r\t,hello egon\t123)) #[‘\n‘] #\d與\D print(re.findall(\d,hello egon 123)) #[‘1‘, ‘2‘, ‘3‘] print(re.findall(\D,hello egon 123)) #[‘h‘, ‘e‘, ‘l‘, ‘l‘, ‘o‘, ‘ ‘, ‘e‘, ‘g‘, ‘o‘, ‘n‘, ‘ ‘] #\A與\Z print(re.findall(\Ahe,hello egon 123)) #[‘he‘],\A==>^ print(re.findall(123\Z,hello egon 123)) #[‘he‘],\Z==>$ #^與$ print(re.findall(^h,hello egon 123)) #[‘h‘] print(re.findall(3$,hello egon 123)) #[‘3‘] # 重復匹配:| . | * | ? | .* | .*? | + | {n,m} | #. print(re.findall(a.b,a1b)) #[‘a1b‘] print(re.findall(a.b,a1b a*b a b aaab)) #[‘a1b‘, ‘a*b‘, ‘a b‘, ‘aab‘] print(re.findall(a.b,a\nb)) #[] print(re.findall(a.b,a\nb,re.S)) #[‘a\nb‘] print(re.findall(a.b,a\nb,re.DOTALL)) #[‘a\nb‘]同上一條意思一樣 #* print(re.findall(ab*,bbbbbbb)) #[] print(re.findall(ab*,a)) #[‘a‘] print(re.findall(ab*,abbbb)) #[‘abbbb‘] #? print(re.findall(ab?,a)) #[‘a‘] print(re.findall(ab?,abbb)) #[‘ab‘] #匹配所有包含小數在內的數字 print(re.findall(\d+\.?\d*,"asdfasdf123as1.13dfa12adsf1asdf3")) #[‘123‘, ‘1.13‘, ‘12‘, ‘1‘, ‘3‘] #.*默認為貪婪匹配 print(re.findall(a.*b,a1b22222222b)) #[‘a1b22222222b‘] #.*?為非貪婪匹配:推薦使用 print(re.findall(a.*?b,a1b22222222b)) #[‘a1b‘] #+ print(re.findall(ab+,a)) #[] print(re.findall(ab+,abbb)) #[‘abbb‘] #{n,m} print(re.findall(ab{2},abbb)) #[‘abb‘] print(re.findall(ab{2,4},abbb)) #[‘abb‘] print(re.findall(ab{1,},abbb)) #‘ab{1,}‘ ===> ‘ab+‘ print(re.findall(ab{0,},abbb)) #‘ab{0,}‘ ===> ‘ab*‘ #[] print(re.findall(a[1*-]b,a1b a*b a-b)) #[]內的都為普通字符了,且如果-沒有被轉意的話,應該放到[]的開頭或結尾 print(re.findall(a[^1*-]b,a1b a*b a-b a=b)) #[]內的^代表的意思是取反,所以結果為[‘a=b‘] print(re.findall(a[0-9]b,a1b a*b a-b a=b)) #[]內的^代表的意思是取反,所以結果為[‘a=b‘] print(re.findall(a[a-z]b,a1b a*b a-b a=b aeb)) #[]內的^代表的意思是取反,所以結果為[‘a=b‘] print(re.findall(a[a-zA-Z]b,a1b a*b a-b a=b aeb aEb)) #[]內的^代表的意思是取反,所以結果為[‘a=b‘] #\# print(re.findall(‘a\\c‘,‘a\c‘)) #對於正則來說a\\c確實可以匹配到a\c,但是在python解釋器讀取a\\c時,會發生轉義,然後交給re去執行,所以拋出異常 print(re.findall(ra\\c,a\c)) #r代表告訴解釋器使用rawstring,即原生字符串,把我們正則內的所有符號都當普通字符處理,不要轉義 print(re.findall(a\\\\c,a\c)) #同上面的意思一樣,和上面的結果一樣都是[‘a\\c‘] #():分組 print(re.findall(ab+,ababab123)) #[‘ab‘, ‘ab‘, ‘ab‘] print(re.findall((ab)+123,ababab123)) #[‘ab‘],匹配到末尾的ab123中的ab print(re.findall((?:ab)+123,ababab123)) #findall的結果不是匹配的全部內容,而是組內的內容,?:可以讓結果為匹配的全部內容 #| print(re.findall(compan(?:y|ies),Too many companies have gone bankrupt, and the next one is my company)) # ===========================re模塊提供的方法介紹=========================== import re #1 print(re.findall(e,alex make love) ) #[‘e‘, ‘e‘, ‘e‘],返回所有滿足匹配條件的結果,放在列表裏 #2 print(re.search(e,alex make love).group()) #e,只到找到第一個匹配然後返回一個包含匹配信息的對象,該對象可以通過調用group()方法得到匹配的字符串,如果字符串沒有匹配,則返回None。 #3 print(re.match(e,alex make love)) #None,同search,不過在字符串開始處進行匹配,完全可以用search+^代替match #4 print(re.split([ab],abcd)) #[‘‘, ‘‘, ‘cd‘],先按‘a‘分割得到‘‘和‘bcd‘,再對‘‘和‘bcd‘分別按‘b‘分割 #5 print(===>,re.sub(a,A,alex make love)) #===> Alex mAke love,不指定n,默認替換所有 print(===>,re.sub(a,A,alex make love,1)) #===> Alex make love print(===>,re.sub(a,A,alex make love,2)) #===> Alex mAke love print(===>,re.sub(^(\w+)(.*?\s)(\w+)(.*?\s)(\w+)(.*?)$,r\5\2\3\4\1,alex make love)) #===> love make alex print(===>,re.subn(a,A,alex make love)) #===> (‘Alex mAke love‘, 2),結果帶有總共替換的個數 #6 obj=re.compile(\d{2}) print(obj.search(abc123eeee).group()) #12 print(obj.findall(abc123eeee)) #[‘12‘],重用了obj

假期(模塊相關)