logging模块介绍
import logging
logging.basicConfig(
# filename=‘access.log‘,
format=‘%(asctime)s - %(name)s - %(levelname)s -%(module)s: %(message)s‘,
datefmt=‘%Y-%m-%d %H:%M:%S %p‘,
level=10
)
logging.debug(‘debug‘) # 10
logging.info(‘info‘) # 20
logging.warning(‘warn‘) #30
logging.error(‘error‘) #40
logging.critical(‘critial‘) #50
日志模块的详细用法:
import logging
#1、Logger:产生日志
logger1=logging.getLogger(‘访问日志‘)
# logger2=logging.getLogger(‘错吴日志‘)
#2、Filter:几乎不用
#3、Handler:接收Logger传过来的日志,进行日志格式化,可以打印到终端,也可以打印到文件
sh=logging.StreamHandler() #打印到终端
fh1=logging.FileHandler(‘s1.log‘,encoding=‘utf-8‘)
fh2=logging.FileHandler(‘s2.log‘,encoding=‘utf-8‘)
#4、Formatter:日志格式
formatter1=logging.Formatter(
fmt=‘%(asctime)s - %(name)s - %(levelname)s -%(module)s: %(message)s‘,
datefmt=‘%Y-%m-%d %H:%M:%S %p‘,
)
formatter2=logging.Formatter(
fmt=‘%(asctime)s : %(message)s‘,
datefmt=‘%Y-%m-%d %H:%M:%S %p‘,
)
formatter3=logging.Formatter(
fmt=‘%(asctime)s : %(module)s : %(message)s‘,
datefmt=‘%Y-%m-%d %H:%M:%S %p‘,
)
#5、为handler绑定日志格式
sh.setFormatter(formatter1)
fh1.setFormatter(formatter2)
fh2.setFormatter(formatter3)
#6、为logger绑定handler
logger1.addHandler(sh)
logger1.addHandler(fh1)
logger1.addHandler(fh2)
#7、设置日志级别:logger对象的日志级别应该<=handler的日志界别
# logger1.setLevel(50)
logger1.setLevel(10) #
sh.setLevel(10)
fh1.setLevel(10)
fh2.setLevel(10)
#8、测试
logger1.debug(‘测试着玩‘)
logger1.info(‘运行还算正常‘)
logger1.warning(‘可能要有bug了‘)
logger1.error(‘不好了,真tm出bug了‘)
logger1.critical(‘完犊子,推倒重写‘)
日志的继承
import logging
#1、Logger:产生日志
logger1=logging.getLogger(‘root‘)
logger2=logging.getLogger(‘root.child1‘)
logger3=logging.getLogger(‘root.child1.child2‘)
#2、Filter:几乎不用
#3、Handler:接收Logger传过来的日志,进行日志格式化,可以打印到终端,也可以打印到文件
sh=logging.StreamHandler() #打印到终端
#4、Formatter:日志格式
formatter1=logging.Formatter(
fmt=‘%(asctime)s - %(name)s - %(levelname)s -%(module)s: %(message)s‘,
datefmt=‘%Y-%m-%d %H:%M:%S %p‘,
)
#5、为handler绑定日志格式
sh.setFormatter(formatter1)
#6、为logger绑定handler
logger1.addHandler(sh)
logger2.addHandler(sh)
logger3.addHandler(sh)
#7、设置日志级别:logger对象的日志级别应该<=handler的日志界别
# logger1.setLevel(50)
logger1.setLevel(10) #
logger2.setLevel(10) #
logger3.setLevel(10) #
sh.setLevel(10)
#8、测试
logger1.debug(‘爷爷‘)
logger2.debug(‘爸爸‘)
logger3.debug(‘孙子‘)
正则模块
import re
# print(re.findall(‘\w‘,‘egon 123 + _ - *‘))
# print(re.findall(‘\W‘,‘egon 123 + _ - *‘))
# print(re.findall(‘\s‘,‘ego\tn 12\n3 + _ - *‘))
# print(re.findall(‘\S‘,‘ego\tn 12\n3 + _ - *‘))
# print(re.findall(‘\d‘,‘ego\tn 12\n3 + _ - *‘))
# print(re.findall(‘\D‘,‘ego\tn 12\n3 + _ - *‘))
# print(re.findall(‘\n‘,‘ego\tn 12\n3 + _ - *‘))
# print(re.findall(‘\t‘,‘ego\tn 12\n3 + _ - *‘))
# print(re.findall(‘e‘,‘ego\tn 12\n3 +hello _ - *‘))
# print(re.findall(‘^e‘,‘ego\tn 12\n3 +hello _ - *‘))
# print(re.findall(‘o$‘,‘ego\tn 12\n3 +hello‘))
#重复:.|?|*|+|{m,n}|.*|.*?
#.代表任意一个字符
# print(re.findall(‘a.b‘,‘a1b a b a-b aaaaaab‘))
# a.b
# print(re.findall(‘a.b‘,‘a1b a b a\nb a-b aaaaaab‘,re.DOTALL))
# a.b
#?:代表?号左边的字符出现0次或者1
# print(re.findall(‘ab?‘,‘a ab abb abbbb a1b‘)) #[‘a‘,‘ab‘,‘ab‘,‘ab‘,‘a‘]
# # ab?
#*:代表*号左边的字符出现0次或者无穷次
# print(re.findall(‘ab*‘,‘a ab abb abbbb a1b‘)) #[‘a‘,‘ab‘,‘abb‘,‘abbbb‘,‘a‘]
# ab*
#+:代表+号左边的字符出现1次或者无穷次
# print(re.findall(‘ab+‘,‘a ab abb abbbb a1b‘)) #[‘ab‘,‘abb‘,‘abbbb‘]
# # ab+
# {m,n}:代表左边的字符出现m次到n次
# print(re.findall(‘ab{0,1}‘,‘a ab abb abbbb a1b‘)) #[‘ab‘,‘abb‘,‘abbbb‘]
# print(re.findall(‘ab?‘,‘a ab abb abbbb a1b‘)) #[‘ab‘,‘abb‘,‘abbbb‘]
# print(re.findall(‘ab{0,}‘,‘a ab abb abbbb a1b‘)) #[‘ab‘,‘abb‘,‘abbbb‘]
# print(re.findall(‘ab*‘,‘a ab abb abbbb a1b‘)) #[‘ab‘,‘abb‘,‘abbbb‘]
# print(re.findall(‘ab{1,}‘,‘a ab abb abbbb a1b‘)) #[‘ab‘,‘abb‘,‘abbbb‘]
# print(re.findall(‘ab+‘,‘a ab abb abbbb a1b‘)) #[‘ab‘,‘abb‘,‘abbbb‘]
# print(re.findall(‘ab{2,4}‘,‘a ab abb abbbb a1b‘)) #[‘abb‘, ‘abbbb‘]
#.*:贪婪匹配
# print(re.findall(‘a.*b‘,‘xxxy123a123b456b‘))
# a.*b
#.*?:非贪婪匹配
# print(re.findall(‘a.*?b‘,‘xxxy123a123b456b‘))
#|:或者
# print(re.findall(‘compan(y|iess)‘,‘too many companiess have gone bankrupt, and the next one is my company‘))
# print(re.findall(‘compan(?:y|iess)‘,‘too many companiess have gone bankrupt, and the next one is my company‘))
# compan(y|iess)
# print(re.findall(‘href="(.*?)"‘,‘<a href="http://www.baidu.com">点击我</a>‘))
#rawstring:
# print(re.findall(r‘a\\c‘,‘a\c a1c aBc‘)) #a\\c->a\c
#[]:取中括号内任意的一个
# print(re.findall(‘a[a-z]b‘,‘axb azb aAb a1b a-b a+b‘))
# print(re.findall(‘a[A-Z]b‘,‘axb azb aAb a1b a-b a+b‘))
# print(re.findall(‘a[a-zA-Z]b‘,‘axb azb aAb a1b a-b a+b‘))
# print(re.findall(‘a[0-9]b‘,‘axb azb aAb a1b a-b a+b‘))
# print(re.findall(‘a[-+*/]b‘,‘axb azb aAb a1b a-b a+b‘))
# print(re.findall(‘a[^-+*/]b‘,‘axb azb aAb a1b a-b a+b‘))
#re模块的其他方法
#re.search :只匹配成功一次就返回
# print(re.search(‘a[*]b‘,‘axb azb aAb a1b a-b a+b‘))
# print(re.search(‘a[0-9]b‘,‘axb azb aAb a1b a-b a2b a+b‘).group())
# re.match:从开头取
# print(re.match(‘a[0-9]b‘,‘axb azb aAb a1b a-b a2b a+b‘))
# print(re.match(‘a[0-9]b‘,‘a1b axb azb aAb a1b a-b a2b a+b‘).group())
# print(re.search(‘^a[0-9]b‘,‘a1b axb azb aAb a1b a-b a2b a+b‘).group())
# re.split
# print(re.split(‘:‘,‘root:x:0:0::/root:/bin/bash‘,maxsplit=1))
# ‘root:x:0:0::/root:/bin/bash‘.split(‘:‘)
# re.sub
# print(re.sub(‘root‘,‘admin‘,‘root:x:0:0::/root:/bin/bash‘,1))
#了解
# print(re.sub(‘^([a-z]+)([^a-z]+)(.*?)([^a-z]+)([a-z]+)$‘,r‘\5\2\3\4\1‘,‘root:x:0:0::/root:/bin/bash‘))
# re.compile
obj=re.compile(‘a\d{2}b‘)
print(obj.findall(‘a12b a123b a12345b abbb‘))
print(obj.search(‘a12b a123b a12345b abbb‘).group())
时间模块
import time
#掌握
print(time.time())
print(time.localtime())
print(time.localtime().tm_mday)
print(time.gmtime())
print(time.strftime(‘%Y-%m-%d %H:%M:%S‘))
print(time.strftime(‘%Y-%m-%d %X‘))
#了解
print(time.localtime(11111111))
print(time.localtime(time.time()))
print(time.gmtime(time.time()))
print(time.mktime(time.localtime()))
print(time.strftime(‘%Y-%m-%d‘,time.localtime()))
print(time.strptime(‘2017-03-01‘,‘%Y-%m-%d‘))
print(time.asctime(time.localtime()))
print(time.ctime(111111))
#datetime
import datetime
print(datetime.datetime.now())
print(datetime.datetime.fromtimestamp(111111111))
print(datetime.datetime.now()+datetime.timedelta(days=3))
print(datetime.datetime.now()+datetime.timedelta(days=-3))
print(datetime.datetime.now()+datetime.timedelta(hours=3))
print(datetime.datetime.now()+datetime.timedelta(minutes=3))
print(datetime.datetime.now()+datetime.timedelta(seconds=3))
print(datetime.datetime.now().replace(year=1999,hour=12))