python 课堂12 模块4

模块4 logging re

logging

import logging
五个日志等级:debug info warning error critical
直接调用日志等级就可以输出日志。

logging.debug('qianleilog....')
logging.warning('qianlei warning...')

设置日志格式

logging.basicConfig
logging.basicConfig(filename='qianlei_test_log.log',format=('%(message)s %(levelname)s %(asctime)s'),datefmt='%Y-%m-%d %X',level=logging.DEBUG)
logging.debug('qianlei_log123...')

常用的日志模块方法:
logger handler formatter

# 获取logger
log_ger = logging.getLogger('第二模块知识点复习')
# 设置logger 等级
log_ger.setLevel(level=logging.DEBUG)
# 获取handler
hs = logging.StreamHandler()
hf = logging.FileHandler('qianlei_test_log.log',encoding='utf8')
# 设置handler等级
hs.setLevel(level=logging.INFO)
hf.setLevel(level=logging.DEBUG)
# 设置 formatter
f_mat = logging.Formatter(fmt=('输出到文件 ---时间:%(asctime)s 日志:%(message)s 等级:%(levelname)s  '),datefmt='%Y-%m-%d %X')
s_mat = logging.Formatter(fmt=('输出到屏幕 ---时间:%(asctime)s 日志:%(message)s 等级:%(levelname)s  '),datefmt='%Y-%m-%d %X')
# 绑定formatter 到 handler
hs.setFormatter(s_mat)
hf.setFormatter(f_mat)
# 绑定handler 到 logger
log_ger.addHandler(hs)
log_ger.addHandler(hf)
# 输入日志
log_ger.debug('qianlei debug 日志文件')
log_ger.info('info 日志文件 ')
log_ger.warning('qianlei warning 日志文件')

输出到屏幕 ---时间:2018-07-11 09:26:03 日志:info 日志文件 等级:INFO
输出到屏幕 ---时间:2018-07-11 09:26:03 日志:qianlei warning 日志文件 等级:WARNING
输出到文件 ---时间:2018-07-11 09:26:03 日志:qianlei debug 日志文件 等级:DEBUG
输出到文件 ---时间:2018-07-11 09:26:03 日志:info 日志文件 等级:INFO
输出到文件 ---时间:2018-07-11 09:26:03 日志:qianlei warning 日志文件 等级:WARNING

re

import re
1、. 匹配任意一个字符
2、^ \A 匹配开头 $ \Z 匹配结尾(必须放在匹配字符的后面)
3、* 0~多次 + 1~多次,? 0~1次
4、{} 指定匹配次数,使用 , 表示范围,{n,m}
5、[] 字符集匹配其中任意一个 里面的 ^ 代表非,不是匹配开头,使用 - 代表范围[0-9]
6、() 分组匹配 结合groups 可以将匹配到内容分开,组成元组
7、| 或 前面或后面的值去匹配。起到匹配时分组的作用。
8、\d 数字 \D 非数字
9、\w 数字字母 \W 非数字字母
10、\s 空字符 \S 非空字符

高级分组匹配语法:
分解身份证号,并为每段号码添加名称
注意:
1、语法中不可以有空格,因为空格本身就是可以匹配的内容
2、每一个单元都在括号中,是分组匹配的高级用法
3、名称的括号是尖括号,因为圆括号在分组时被用掉了
3、p 是大写

s = '320821201807110138'
res = re.search('(?P<省份>\d{3})(?P<市>\d{3})(?P<年>\d{4})(?P<月>\d{2})(?P<日>\d{2})',s)
print(res.groupdict())

匹配方法:

1、match

s = 'qianlei shi ge shuaige'
从开头匹配,开头没有就找不到
m_res = re.match('[a-z]+',s)
print(m_res)
print(m_res.group)
s = 'qianlei shi ge shuaige'
全局找,找到一个就结束了。
查找的规则必须连着,分组groups 只接收()内的内容。
r_res = re.search('([a-z])+\s([a-z]+)\s[a-z]+',s)
print(r_res)
print(r_res.group())
print(r_res.groups()) # 只接收()分组中的值。
<re.Match object; span=(0, 14), match='qianlei shi ge'>
qianlei shi ge
('i', 'shi')

3、findall()

s = 'qianlei shi ge shuaige'
全局找,所有符合的内容全部返回
直接返回找到的元素组成的列表。贪婪匹配。
f_res = re.findall('[a-z]+',s)
print(f_res)

4、compile

s = 'qianlei shi ge shuaige'
可以重复使用已经编译好的匹配对象,提高调用效率。
c = re.compile('[a-z]+') # 先编译匹配对象
res = c.findall(s)  # 使用匹配对象直接调用 匹配方法去匹配,等同于 re.findall('[a-z]+',s)
print(res)
['qianlei', 'shi', 'ge', 'shuaige']

5、split 分割

比字符串自带的split()方法高级,可以模糊查找
s = 'qianlei shi ge shuaige'
匹配分割内容,按匹配到的内容分割字符串
res = re.split('\s',s)
print(res)
['qianlei', 'shi', 'ge', 'shuaige']

6、sub 替换

比字符串自带的replace() 高级
s = 'qianlei shi ge shuaige'
res = re.sub('\s','_',s)
print(res)
qianlei_shi_ge_shuaige

7、fullmatch

完全匹配,必须把对应的字符串全部匹配上,才会返回match对象,否则返回None.
s = 'qianlei shi ge shuaige'
res = re.fullmatch('[a-z]+\s[a-z]+\s[a-z]+\s[a-z]+',s)
print(res)
print(res.group())
<re.Match object; span=(0, 22), match='qianlei shi ge shuaige'>
qianlei shi ge shuaige

flags 标志符

re.I # 忽略大小写

s = 'Qianlei Shi Ge Shuaige'
print(re.search('[a-z]+',s))
print(re.search('[a-z]+',s,flags=re.I))
<re.Match object; span=(1, 7), match='ianlei'>
<re.Match object; span=(0, 7), match='Qianlei'>

M 多行模式

s = 'qian\nlei\n'
res = re.search('[a-z]+.$',s) # 整个字符串当一行,匹配结尾
print(res.group())
lei
res = re.search('[a-z]+.$',s,flags=re.M) # 多行模式,匹配上一行的结尾
print(res.group())
qian

S 改变点 . 的行为

print(re.search('.','\n')) # 匹配不到特殊字符
None
print(re.search('.','\n',flags=re.S))
<re.Match object; span=(0, 1), match='\n'> # 让 . 可以匹配特殊字符

X 可以给表达式写注释。

把注释内容也当做匹配内容
print(re.search('\d+# 这个是注释','123ABC'))
None

添加re.X后可以直接忽略注释内容
print(re.search('\d+# 这个是注释','123ABC',re.X))
<re.Match object; span=(0, 3), match='123'>

猜你喜欢

转载自www.cnblogs.com/qianduoduo123/p/9333045.html