一、re:正則表達式
正則就是用一些具備特殊含義的符號組合到一塊兒(稱爲正則表達式)來描述字符或者字符串的方法。或者說:正則就是用來描述一類事物的規則。(在Python中)它內嵌在Python中,並經過 re 模塊實現。正則表達式模式被編譯成一系列的字節碼,而後由用 C 編寫的匹配引擎執行。函數
二、findall 所有找到返回一個列表:spa
s = "alex,meet,eva_j"
import re
print(re.findall('e',s)) # 參數1:要查找的內容,參數2:從哪查找,三、返回結果是列表的形式
結果:['e', 'e', 'e', 'e']
三、\w 匹配中文,字母,數字,下劃線:
import re
s = "alex1!,你哈t2,eva_j@!"
print(re.findall("\w",s)) # 匹配字母(包含中文)或數字或下劃線 ***
結果:['a', 'l', 'e', 'x', '1', '你', '哈', 't', '2', 'e', 'v', 'a', '_', 'j']
四、\W 不匹配中文,字母,數字,下劃線:
import re
s = "alex1!,你哈t2,eva_j@!"
print(re.findall("\W",s)) # 匹配非字母(包含中文)或數字或下劃線 ***
結果:['!', ',', ',', '@', '!']
五、\s 匹配任意的空白符:
import re
s = " zhangda@, "
print(re.findall("\s",s)) # 匹配任意空白符
結果:[' ', ' ']
六、\S 匹配不是任意的空白符:
import re
s = " zhangda@, "
print(re.findall("\S",s)) # 匹配任意非空白符包含特殊符號 ***
結果:['z', 'h', 'a', 'n', 'g', 'd', 'a', '@', ',']
七、\d 匹配數字:
import re
s = " 123zhangda@, "
print(re.findall('\d',s)) # 匹配數字 ***
結果:['1', '2', '3']
八、\D 匹配非數字:
import re
s = " 123zhangda@, "
print(re.findall('\D',s))
結果:[' ', 'z', 'h', 'a', 'n', 'g', 'd', 'a', '@', ',', ' ']
九、從字符串開頭匹配:
import re
s = "Adsasf"
print(re.findall("\AA",s)) #\A從字符串開頭匹配、從字符串開頭匹配沒有就返回空列表
結果:['A']
十、^匹配字符串的開始:
import re
s = "Adsasf"
print(re.findall("^A",s)) # 匹配開頭沒有就返回空列表 ***
結果:['A']
十一、\Z匹配字符串的結束,若是是換行,只匹配到換行前的結果:
import re
s = "Adsasf"
print(re.findall("f\Z",s))
結果:['f']
十二、\$匹配字符串的結尾:
import re
s = "Adsasf"
print(re.findall("f$",s)) # 匹配結尾 ***
結果:['f']
1三、.匹配任意字符,除了換行符,當re.DOTALL標記被指定時,則能夠匹配包括換行符的任意字符。
import re
s = "!@#$5%0,1,2AAAA,113,-4,5,-6,7,9alex_meet\n"
print(re.findall(".",s)) # 匹配任意字符串(換行和製表符除外) ***
結果:['!', '@', '#', '$', '5', '%', '0', ',', '1', ',', '2', 'A', 'A', 'A', 'A', ',', '1', '1', '3', ',', '-', '4', ',', '5', ',', '-', '6', ',', '7', ',', '9', 'a', 'l', 'e', 'x', '_', 'm', 'e', 'e', 't']
print(re.findall(".",s,re.DOTALL))
結果:['!', '@', '#', '$', '5', '%', '0', ',', '1', ',', '2', 'A', 'A', 'A', 'A', ',', '1', '1', '3', ',', '-', '4', ',', '5', ',', '-', '6', ',', '7', ',', '9', 'a', 'l', 'e', 'x', '_', 'm', 'e', 'e', 't', '\n']
1四、[...]匹配字符組中的字符:
import re
s = "!@#$5%0,1,2AAAA,113,-4,5,-6,7,9alex_meet\n"
print(re.findall("[0-9a-zA-Z]",s)) # [數字0-數字9和字母a-z和A-Z]
結果:['5', '0', '1', '2', 'A', 'A', 'A', 'A', '1', '1', '3', '4', '5', '6', '7', '9', 'a', 'l', 'e', 'x', 'm', 'e', 'e', 't']
import re
s = "!@#$5%0,1,2AAAA,113,-4,5,-6,7,9alex_meet\n"
print(re.findall("[0-9]",s)) # [數字0-數字9]
結果:['5', '0', '1', '2', '1', '1', '3', '4', '5', '6', '7', '9']
1五、*匹配0個或者多個左邊的字符 貪婪匹配方式:
import re
s = "aleex1.!,你哈t2,eev11a_j@!1111"
print(re.findall("a*",s))
結果:['a', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', 'a', '', '', '', '', '', '', '', '', '']
1六、+匹配一個或者多個左邊的字符貪婪匹配方式:
import re
s = "aleex1.!,你哈t2,eev11a_j@!1111"
print(re.findall("a+",s))
結果:['a', 'a']
1七、?匹配0個或者1個左邊的字符,非貪婪方式:
import re
s = "aleex1.!,你哈t2,eev11a_j@!1111"
print(re.findall("a?",s))
結果:['a', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', 'a', '', '', '', '', '', '', '', '', '']
1八、{n}精準匹配n個前面的表達式:
import re
s1 = "aleex1.!,你哈t2,eev11a_j@!1111"
print(re.findall("e{2}",s1)) # 精確
print(re.findall("ee",s1))
結果:['ee', 'ee']
1九、{n,m}匹配n到m次由前面的正則表達式定義的片斷,貪婪方式:
import re
s1 = "aleex1.!,你哈t2,eev11a_j@!1111"
print(re.findall('e{0,3}',s1)) # 範圍
結果:['', '', 'ee', '', '', '', '', '', '', '', '', '', '', 'ee', '', '', '', '', '', '', '', '', '', '', '', '', '']
20、a|b 匹配a或者b:
import re
s1 = "alea121meeeeeeeet11123,wusir324"
print(re.findall("a|e",s1)) # 或
結果:['a', 'e', 'a', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e']
2一、() 匹配括號內的表達式,也表示一個組:
import re
s1 = "alea121meeeeeeeet11123,wusir324"
print(re.findall("e(e)e",s1)) # 分組
print(re.findall("m(eee)e",s1)) # 分組
print(re.findall("e(?:e)e",s1)) # 分組
結果: ['e', 'e']
['eee']
['eee', 'eee'].net
2二、找到全部帶_sb的內容:線程
s = 'alex_sb wusir_sb 的 alex wusir '
import re
print(re.findall("(.*)_sb",s))
print(re.findall("(.+?)_sb",s)) # .... * 0 or .... 1
print(re.findall("(.............)_sb",s))
結果: ['alex_sb wusir']
['alex', ' wusir']
['alex_sb wusir']debug
2三、\d匹配數字:調試
import re
s1 = "1-2*(60+(-40.35/5)-(-4*3))"
print(re.findall("\d+",s1))
結果:['1', '2', '60', '40', '35', '5', '4', '3']
2四、\. == 轉義成普通的小數點:
import re
s1 = "1-2*(60+(-40.35/5)-(-4*3))"
print(re.findall("\d+\.\d+|\d+",s1))
結果:['1', '2', '60', '40.35', '5', '4', '3']
2五、匹配全部的數字(包含小數包含負號):
print(re.findall("-\d+\.\d+|-\d+|\d+",s1))
結果:['1', '-2', '60', '-40.35', '5', '-4', '3']
2六、匹配全部郵箱:
import re
s2 = "http://blog.csdn.net/make164492212@163.com/article/details/51656638" # 匹配全部郵箱
print(re.findall('\w+@\d+\.com',s2))
結果:['make164492212@163.com']
2七、匹配qq號:騰訊從10000開始:
import re
s6 = "1231231,324233,123,1123,2435,1234,2546,23451324,3546354,13241234"
print(re.findall('\d{5,11}',s6))
結果:['1231231', '324233', '23451324', '3546354', '13241234']
2八、search、match:
search ***** # 從字符串任意位置進行匹配,查找到一個就中止了
match ***** # 從字符串開始位置進行匹配,找不返回None
2九、split分割:
import re
print(re.split('[ ::,;;,]','alex wusir,日天,太白;女神;肖鋒:吳超'))
結果:['alex', 'wusir', '日天', '太白', '女神', '肖鋒', '吳超']
30、sub替換:
import re
s = 'barry是最好的講師,barry就是一個普通老師,請不要將barry當男神對待。'
print(re.sub('barry', 'meet',s))
結果:meet是最好的講師,meet就是一個普通老師,請不要將meet當男神對待。
3一、compile 定義匹配規則:
import re
obj = re.compile('\d{2}')
print(obj.findall("alex12345"))
結果:['12', '34']
3二、給分組起名字:
import re
s = "<h1>hello</h1>"
print(re.findall("<(\w+)>",s))
結果:h1
3三、logging模塊:
logging -- 日誌
1.記錄程序運行狀態
# 時間,那個文件,報錯行數,錯誤信息
2.用戶的喜愛
# 分析用戶的一些喜愛,操做
3.銀行
# 帳戶的一些流水
咱們來講一下這個logging模塊,這個模塊的功能是記錄咱們軟件的各類狀態,大家如今和我一塊兒找到紅蜘蛛的那個圖標,而後右鍵找一找是否是有個錯誤日誌.其實每一個軟件都是有錯誤日誌的,開發人員能夠經過錯誤日誌中的內容對他的程序進行修改日誌
這只是一種應用場景,有的還會將日誌用於交易記錄.好比你給我轉帳應該作記錄吧,code
咱們使用的信用卡,每消費的一筆都會記錄,咱們來看看這個日誌怎麼用?orm
咱們先來看一下函數式簡單配置
import logging logging.debug('debug message') logging.info('info message') logging.warning('warning message') logging.error('error message') logging.critical('critical message')
默認狀況下Python的logging模塊將日誌打印到了標準輸出中,且只顯示了大於等於WARNING級別的日誌,這說明默認的日誌級別設置爲WARNING
(日誌級別等級CRITICAL > ERROR > WARNING > INFO > DEBUG),
默認的日誌格式爲日誌級別:Logger名稱:用戶輸出消息。
咱們本身用函數寫的這個能夠正常使用可是不夠靈活,咱們看看這個靈活的
靈活配置日誌級別,日誌格式,輸出位置:
import logging logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s', datefmt='%a, %d %b %Y %H:%M:%S', filename='/tmp/test.log', filemode='w') logging.debug('debug message') logging.info('info message') logging.warning('warning message') logging.error('error message') logging.critical('critical message')
logging.debug("這是調試")
logging.info("這是信息")
logging.warning("這是警告")
logging.error("這是錯誤")
logging.critical("這是危險")
basicConfig()函數中可經過具體參數來更改logging模塊默認行爲,可用參數有:
format參數中可能用到的格式化串:
logger對象配置
import logging
logger = logging.getLogger()
# 建立一個handler,用於寫入日誌文件
fh = logging.FileHandler('test.log',encoding='utf-8')
# 再建立一個handler,用於輸出到控制檯
ch = logging.StreamHandler()
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
fh.setLevel(logging.DEBUG)
fh.setFormatter(formatter)
ch.setFormatter(formatter)
logger.addHandler(fh) #logger對象能夠添加多個fh和ch對象
logger.addHandler(ch)
logger.debug('logger debug message')
logger.info('logger info message')
logger.warning('logger warning message')
logger.error('logger error message')
logger.critical('logger critical message')
logging庫提供了多個組件:Logger、Handler、Filter、Formatter。Logger對象提供應用程序可直接使用的接口,Handler發送日誌到適當的目的地,Filter提供了過濾日誌信息的方法,Formatter指定日誌顯示格式。另外,能夠經過:logger.setLevel(logging.Debug)設置級別,固然,也能夠經過
fh.setLevel(logging.Debug)單對文件流設置某個級別。
import logging
looger = logging.getLogger() # 建立一個空架子
fh = logging.FileHandler('test1.log',mode="a",encoding="utf-8")
# 建立一個文件句柄,用來記錄日誌(文件流)
ch = logging.StreamHandler()
# 建立一個屏幕流,打印記錄的內容
f_str = logging.Formatter("%(asctime)s %(name)s %(levelname)s %(filename)s %(lineno)s %(message)s")
# 定義一個記錄日誌的格式
looger.level = 10
# 設置一個記錄級別
fh.setFormatter(f_str) # 給文件句柄設置記錄內容的格式
ch.setFormatter(f_str) # 給中控臺設置打印內容的格式looger.addHandler(fh) # 將文件句柄添加的looger對象中looger.addHandler(ch) # 將中控臺添加的looger對象中looger.debug(1234) # 我們二次開發實現的looger.info(1234) # 我們二次開發實現的looger.warning(1234) # 我們二次開發實現的looger.error(1234) # 我們二次開發實現的looger.critical(1234) # 我們二次開發實現的"""# logging.debug() # 人家自帶的