假期(模块相关)
# ----------------------------------------------------------------------------------------
import time
timestamp = time.time() #時(shí)間戳
struct_time = time.localtime() #結(jié)構(gòu)化時(shí)間
format_time = time.strftime("%Y-%m-%d %X") #格式化時(shí)間# print(time.localtime(timestamp)) # 時(shí)間戳 ====== > 結(jié)構(gòu)化時(shí)間
# print(time.mktime(struct_time)) # 結(jié)構(gòu)化時(shí)間 ======> 時(shí)間戳# print(time.strftime("%Y-%m-%d %X"),struct_time) #格式化時(shí)間 ===== > 結(jié)構(gòu)化時(shí)間
# print(time.strptime("2018-02-13 09:25:45","%Y-%m-%d %X")) #結(jié)構(gòu)化時(shí)間 === > 格式化時(shí)間# 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的隨機(jī)小數(shù)
# print(random.randint(1,4)) # 產(chǎn)生大于等于1,小于等于4的整數(shù)
# print(random.randrange(1,4)) # 產(chǎn)生大于等于1,小于4的整數(shù)
# print(random.choice([1,"23",[4,5]])) #隨機(jī)產(chǎn)生列表中的一項(xiàng)
# print(random.sample([1,"23",[4,5],2],2)) #第一個(gè)參數(shù)是列表,第二個(gè)參數(shù)是隨便產(chǎn)生的列表項(xiàng)
# print(random.uniform(1,3)) #產(chǎn)生1-3之間的隨機(jī)小數(shù)# item = [1,2,3,4,5,6]
# random.shuffle(item) #打亂原列表的順序,沒有返回值
# print(item)
# 生成隨機(jī)驗(yàn)證碼
# 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()) #獲取當(dāng)前工作目錄
# print(os.chdir("dirname")) #相當(dāng)于cd切換命令
# os.remove("dirname") #刪除dirname目錄
# os.rename("oldname","newname") #重命名
# print(os.environ) #獲取當(dāng)前的環(huán)境變量
# os.path.join("dirname") #路徑拼接# ----------------------------------------------------------------------------------------
import sys
# print(sys.argv) #當(dāng)前程序的路徑
# print(sys.exit(0)) #退出程序
# print(sys.version) #獲取python的版、本信息
# print(sys.maxsize) #獲取最大int值
# print(sys.path) #返回模塊的搜索路徑
# print(sys.platform) #返回操作系統(tǒng)的名稱
# 實(shí)現(xiàn)打印進(jìn)度條
# 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") #將文件內(nèi)容拷貝到另一個(gè)文中
# shutil.copymode("file1.log","file2.log") #僅僅拷貝權(quán)限 file2.log 必須存在
# shutil.copystat("file1","file2") #拷貝狀態(tài)信息,file2必須存在
# shutil.copy("file1.log","file2.log") #拷貝文件和權(quán)限
# shutil.copy2("file1.log","file2.log") #拷貝文件和狀態(tài)信息
# 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數(shù)據(jù)主要用于跨平臺數(shù)據(jù)交互
# 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不認(rèn)單引號,json可以跨平臺交互,pickle只可以在python中數(shù)據(jù)交互
# import pickle
# dic = {'name':'alvin','age':23,'sex':'male'}
# j = pickle.dumps(dic)
# print(type(j)) #<class 'bytes'># ----------------------------------------------------------------------------------------
import shelve
# shelve模塊比pickle模塊更加的簡單,只有一個(gè)open函數(shù),返回類字典對象,可讀可寫,key必須為字符串,值可以是python所支持的數(shù)據(jù)類型
# 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 #這個(gè)模塊以及過時(shí),已經(jīng)被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 dicdef 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']# 重復(fù)匹配:| . | * | ? | .* | .*? | + | {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']
#匹配所有包含小數(shù)在內(nèi)的數(shù)字
print(re.findall('\d+\.?\d*',"asdfasdf123as1.13dfa12adsf1asdf3")) #['123', '1.13', '12', '1', '3']#.*默認(rèn)為貪婪匹配
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')) #[]內(nèi)的都為普通字符了,且如果-沒有被轉(zhuǎn)意的話,應(yīng)該放到[]的開頭或結(jié)尾
print(re.findall('a[^1*-]b','a1b a*b a-b a=b')) #[]內(nèi)的^代表的意思是取反,所以結(jié)果為['a=b']
print(re.findall('a[0-9]b','a1b a*b a-b a=b')) #[]內(nèi)的^代表的意思是取反,所以結(jié)果為['a=b']
print(re.findall('a[a-z]b','a1b a*b a-b a=b aeb')) #[]內(nèi)的^代表的意思是取反,所以結(jié)果為['a=b']
print(re.findall('a[a-zA-Z]b','a1b a*b a-b a=b aeb aEb')) #[]內(nèi)的^代表的意思是取反,所以結(jié)果為['a=b']#\# print(re.findall('a\\c','a\c')) #對于正則來說a\\c確實(shí)可以匹配到a\c,但是在python解釋器讀取a\\c時(shí),會發(fā)生轉(zhuǎn)義,然后交給re去執(zhí)行,所以拋出異常
print(re.findall(r'a\\c','a\c')) #r代表告訴解釋器使用rawstring,即原生字符串,把我們正則內(nèi)的所有符號都當(dāng)普通字符處理,不要轉(zhuǎn)義
print(re.findall('a\\\\c','a\c')) #同上面的意思一樣,和上面的結(jié)果一樣都是['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的結(jié)果不是匹配的全部內(nèi)容,而是組內(nèi)的內(nèi)容,?:可以讓結(jié)果為匹配的全部內(nèi)容#|
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'],返回所有滿足匹配條件的結(jié)果,放在列表里
#2
print(re.search('e','alex make love').group()) #e,只到找到第一個(gè)匹配然后返回一個(gè)包含匹配信息的對象,該對象可以通過調(diào)用group()方法得到匹配的字符串,如果字符串沒有匹配,則返回None。#3
print(re.match('e','alex make love')) #None,同search,不過在字符串開始處進(jìn)行匹配,完全可以用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,默認(rè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 alexprint('===>',re.subn('a','A','alex make love')) #===> ('Alex mAke love', 2),結(jié)果帶有總共替換的個(gè)數(shù)#6
obj=re.compile('\d{2}')print(obj.search('abc123eeee').group()) #12
print(obj.findall('abc123eeee')) #['12'],重用了obj
?
轉(zhuǎn)載于:https://www.cnblogs.com/52-qq/p/8446945.html
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎總結(jié)
- 上一篇: 简单好用的计算器:bc
- 下一篇: Windows 10 IoT Core