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()) #要验证密码准确性就使用密文互相比较

浙公网安备 33010602011771号