--coding:utf-8--
1.正則表達(dá)式(Regular Expression)
2.Re正則表達(dá)式模塊
'''
python的內(nèi)置模塊,使用前 import re
表示方法:r'\d{3}\s+\d{3,8}'
2.2常用函數(shù)
re.complie(patern,flag = 0) :將字符串的正則表達(dá)式編譯為Pattern對象
re.search(string[,pos[,endpos]]) 從string的任意位置開始匹配
re.match(string[,pos[,endpos]]) 從string的開頭開始匹配 返回Match對象,否則返回None
re.findall(string[,pos[,endpos]]) 從string的任意位置開始匹配。返回一個列表
re.finditer(string[,pos[,endpos]]) 從string的任意位置開始匹配,返回一個迭代器
'''
import re
obj = re.match(r'\d{3}-\d{3,8}$','010-287688')
print(obj)
切分字符串 split()無法識別連續(xù)空格,使用re.split()+正則表達(dá)式組合
s = 'a b c d e r'
list01 = re.split(r'[\s,;]+',s)
list02 = s.split(' ')
print(list01)
print(list02)
3.logging日志模塊
'''
python 內(nèi)置標(biāo)準(zhǔn)庫,用于輸出運(yùn)行日志,可以設(shè)置日志級別,日志保存路徑,日志文件回滾
import logging
日志級別:使用范圍,由高到低
1.FATAL:致命錯誤
2.CRITICAL:特別糟糕的,如內(nèi)存耗盡,磁盤為空,一般很少用
3.ERROR:發(fā)生錯誤時,如IO操作失敗或者連接問題
4.WARNING:發(fā)生很重要的時間,并不是錯誤
5.INFO:處理請求或者狀態(tài)變化等日常事務(wù)
6.DEBUG:調(diào)試過程使用DEBUG等級
備注:5,6使用print()
'''
3.3使用
'''
1.設(shè)置日志首先需要使用logging.basiceConfig()函數(shù)設(shè)置日志信息的輸出格式
基本語法:
logging.basicConfig(level = ?,format = ?)
level:日志級別
format:設(shè)置信息格式,如下
%(asctime)s:系統(tǒng)內(nèi)時間
%(name)s:信息源名稱
%(levelname)s:信息級別
%(message)s:信息內(nèi)容
2.設(shè)置信息源名稱,返回一個logger對象
logger = logging.getLogger(name)#name 系統(tǒng)級變量獲取運(yùn)行對象名稱
'''
import logging
配置日志輸出格式
logging.basicConfig(level = logging.INFO,format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger(name)
logger.fatal('系統(tǒng)奔潰或發(fā)生致命錯誤,導(dǎo)致程序中斷時需要輸出的信息')
logger.critical('系統(tǒng)資源耗竭時需要輸出的信息')
logger.error('系統(tǒng)報錯異常時需要輸出的信息')
logger.warning('系統(tǒng)運(yùn)行警告時需要輸出的信息')
logger.info('一般信息數(shù)據(jù)')
logger.debug('測試調(diào)試時需要輸出的信息數(shù)據(jù)')
日志默認(rèn)從info級別向上輸出,debug不輸出,太多
3日志寫入文件
'''
FileHandle 用來將輸出日志信息寫入指定文件
步驟:
3.1 創(chuàng)建并設(shè)置logger
3.2 創(chuàng)建并設(shè)置FileHandle對象
3.3logger對象重新添加并替換原有的Handler處理器
'''
import logging
import os
import time
loger01 = logging.getLogger(name)
loger01.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
dirPath = os.path.join(os.getcwd(),'測試用例')
if not os.path.exists(dirPath):
os.mkdir(dirPath)
logFileName = time.strftime('%Y%m%d%H%M%S',time.localtime()) + 'log'
logPath = dirPath + os.sep + logFileName
fileHandle = logging.FileHandler(logPath)
fileHandle.setLevel(logging.INFO)
fileHandle.setFormatter(formatter)
loger01.addHandler(fileHandle)
loger01.fatal('系統(tǒng)奔潰或發(fā)生致命錯誤,導(dǎo)致程序中斷時需要輸出的信息')
loger01.critical('系統(tǒng)資源耗竭時需要輸出的信息')
loger01.error('系統(tǒng)報錯異常時需要輸出的信息')
loger01.warning('系統(tǒng)運(yùn)行警告時需要輸出的信息')
loger01.info('一般信息數(shù)據(jù)')
loger01.debug('測試調(diào)試時需要輸出的信息數(shù)據(jù)')