1. 程式人生 > 實用技巧 >Py-re正則模組,log模組,config模組,雜湊加密

Py-re正則模組,log模組,config模組,雜湊加密

9.re正則表示式模組,用於字串的模糊匹配

元字元:

第一:點為萬用字元

用.表示匹配除了換行符以外的所有字元
import re
res=re.findall('a..x','adsxwassxddfr')
print(res)

第二:^來匹配最開始的部位

import re
res=re.findall('^a..s','asdswassxddfr')
print(res)

第三:$來匹配最末尾的部位

import re
res=re.findall('a..s$','wassxddfrasds')
print(res)

第四:*用於匹配和前面的字元重複了多少次(0到無窮次)

import re
res=re.findall('as*','wassssbewq')   
print(res)
res=re.findall('s*','wassssbewq')  #沒有s也能匹配到空值
print(res) 

第五:+用於匹配和前面的字元重複了多少次(1到無窮次)

import re
res=re.findall('as+','wassssbewq')
print(res)
res=re.findall('s+','wassssbewq')   #必須要s才能匹配到
print(res)

第六:?用於匹配和前面的字元重複了多少次(0到1)

import re
res
=re.findall('as?','wassssbewq') #多少個重複的都只取1個,去重 print(res) res=re.findall('s?','wassssbewq') #沒有s也能匹配到空值 print(res)

第七:{}用於匹配和前面的字元重複了多少次

範圍0到無窮 {0,}
範圍1到無窮 {1,}
範圍0到1 {0,1}
範圍固定次數{固定次數}
import re
res=re.findall('as{0,}','wassssbewq')  #多少個重複的都只取1個,去重
print(res)
res=re.findall('s{6}','wassssssbewq')   #
沒有s也能匹配到空值 print(res)

第八:[]字符集

[]字符集裡面除了-,^,\
import re
res=re.findall('b[ef]w','wassssbewqbfw')   #e或f
print(res)
res=re.findall('b[a-z]w','wassssbewqbfw')   #區間a到z
print(res)
res=re.findall('b[^a-z]w','wabewqbfwb3w') #[]裡面的^是除了集合內這個元素以外的都匹配
print(res)
#如何找出這條算式最裡層的括號
res=re.findall('\([^()]*\)','12+(34+6-2*(2-1))') #()的前面加上\可以轉換成沒有功能的普通括號
print(res)  #要找出的是一個裡面沒有括號的括號,首先識別出括號\( \)
#然後再中間用集合去除掉括號內還有括號的情況,然後加個*來弄多個字串的內容

第九:前面的*,+,?等都是貪婪匹配,按最大的去匹配

在後面加上一個?可以使其變成惰性匹配
import re
res=re.findall('as*?','wassssbewq')  #*的區間是0-無窮,惰性後s顯示0次
print(res)
res=re.findall('as+?','wassssbewq')   #+的區間是1-無窮,惰性後s顯示1次
print(res)

第十:\用於把有功能的元字元變成普通的字元,使其失去功能。還有獲取功能

\d相當於任意十進位制數
\D相當於任意非數字字元
\s相當於空白字元
\S相當於非空白字元
\w相當於字母數字字元
\W相當於非字母數字字元
\b相當於任意特殊字元邊界,比如空格,&,#等等
import re
res=re.findall('\d','12+(34+6-2*(2-1))')
print(res)
res=re.findall('\D','12+(34+6-2*(2-1))')
print(res)
#如果需要匹配斜槓使用四個斜槓
res=re.findall('c\\\\l','c\laa')
print(res) 
#顯示的時候會出現兩個斜槓,但是沒關係,只要能匹配成功就能正常使用判斷

第十一:分組和或,search

import re
res=re.findall('as|b','wassssabewq') 
print(res)
#可以看見此時的as和b是分開的,可以使用括號分組來解決這個問題
res=re.findall('a(s|b)','wassssabewq')  #這裡只顯示了s和b,因為它優先顯示組內的,其它不顯示
print(res)
#可以用:?去優先順序顯示全部
res=re.findall('a(?:s|b)','wassssabewq')  #這裡只顯示了s和b,因為它優先顯示組內的,其它不顯示
print(res)
#也可以用search來分組,但是search只能匹配一次
res=re.search('(?P<alpha>[a-z]+)(?P<number>\d+)','abcd123we2').group('number')
print(res)

第十二:match,只會從開始進行匹配

第十三:分割split

import re
res=re.split('[ |]','hello abc|asd')
print(res)

第十四:替換操作sub

import re
res=re.sub('\d','A','SD123FS23') #將裡面的數字替換成A
print(res)
res=re.sub('\d+','A','SD123FS23')
print(res)

第十五:替換操作subn,並且計算出替換的次數

import re
res=re.subn('\d','A','SD123FS23') #將裡面的數字替換成A
print(res)

第十六:通過編譯來呼叫

import re
com=re.compile('\d+')
res=com.findall('sdadw234dsa13')
print(res)

第十七:放進迭代器

import re
res=re.finditer('\d+','sdadw234dsa13')
for i in res:
    print(i)

10.logging模組,日誌模組

import logging
#以下為日誌級別
logging.debug('debug message')
logging.info('info message')
logging.warning('warning message')
logging.error('error message')
logging.critical('critical message')

以上顯示的時候會發現只顯示出了下面三行,因為預設階級是warning級別
它只顯示比warning更危險的級別的
可以按下面改變優先順序
import logging
#以下為改變優先順序
logging.basicConfig(
    level=logging.DEBUG,
    filename='日誌檔案',  #把日誌記錄放進新建的日誌txt
    filemode='w',  #寫入模式,會覆蓋之前的,永遠都是五條,如果不寫filemode追加
    #模式,寫入的日誌記錄會疊加
    format='%(asctime)s %(lineno)d %(message)s'  #時間  行號   日誌內容
)
logging.debug('debug message')
logging.info('info message')
logging.warning('warning message')
logging.error('error message')
logging.critical('critical message')

日誌檔案不僅向其他檔案傳送日誌記錄,還同時向當前的螢幕傳送
import logging
logger=logging.getLogger()
logger.setLevel('DEBUG')
fh=logging.FileHandler('日誌檔案1')
ch=logging.StreamHandler()
fm=logging.Formatter('%(asctime)s %(message)s')
fh.setFormatter(fm)
ch.setFormatter(fm)
logger.addHandler(fh)
logger.addHandler(ch)
logger.debug('debug message')
logger.info('info message')
logger.warning('warning message')
logger.error('error message')
logger.critical('critical message')

11.configparse模組,生成配置檔案

import configparser
config= configparser.ConfigParser()
config['DEFAULT']={'serve':'123.45.78:56654', #default為主鍵值
                               'name':'abcd'  #name,serve子鍵值對
}
config['mod1']={}
topsecret=config['mod1']  #對空的進行新增操作
topsecret['serve']='111.15.178:2224'
topsecret['name']='kasading'
configfile = open ('configexmple.ini','w')
config.write(configfile)
configfile.close()
#——————查——————
config.read('configexmple.ini')
print(config.sections())   #注意,預設是不顯示的
print(config.defaults())   #可以列印預設裡面的內容
print('ss' in config)  #可以判斷某個字串在不在congfig的主鍵值中
print('mod1' in config)
print(config['mod1']['serve']) #可以像字典一樣取內部
print(config.get('mod1','serve'))#連續取值,和上面的那個一樣
print(config.options('mod1'))
print(config.items('mod1'))
#——————增——————
config.add_section('m870') #增加主鍵
config.set('m870','serve','111.222.333:27015')
config.set('m870','name','sb1')  #增加子鍵值對
configfile1 = open ('configexmple1.ini','w')
config.write(configfile1)
configfile1.close()
#——————刪——————
config.remove_section('mod1')  #刪除主鍵
config.remove_option('DEFAULT','serve')  #刪除defaul下面的serve
configfile2 = open ('configexmple2.ini','w')
config.write(configfile2)
configfile2.close()

12.hashlib模組,摘要演算法,用於明文變成密文

import hashlib
data=input("想要加密的資料")
data1='sb'+data   #要在前面加上自己定製的字元,防止反解
obj=hashlib.md5(data1.encode('utf8'))  #md5還可以換成SHA256,SHA384
print(obj.hexdigest())
#要驗證密碼準確性就使用密文互相比較