Python之路,day6-Python基础
1.config 模块
1 import configparser 2 3 conf = configparser.ConfigParser() 4 conf["DEFAULT"] = {'ServerAliveInterval': '45', 5 'Compression': 'yes', 6 'CompressionLevel': '9'} 7 conf['wwwwwwwww'] = {} 8 conf['wwwwwwwww']['user'] = 'baidu' 9 conf['topsecret.server.com'] = {} 10 topsecret = conf['topsecret.server.com'] 11 topsecret['Host Port'] = '50022' 12 with open('conf.ini','w') as f: 13 f.write(conf)
2.hashlib操作
1 import hmac 2 h = hmac.new(b'hsc') 3 h.update(b'12233') 4 print(h)
3.random模块
1 import random 2 print(random.random()) 3 print(random.randint(1,2)) #0,1,2 随机 4 print(random.randrange(1,2))#0,1 随机 5 6 checkcode = '' 7 for i in range(6): 8 current = random.randrange(0,6) 9 if current != i: 10 temp = chr(random.randint(65,90)) 11 else: 12 temp = random.randint(0,9) 13 checkcode += str(temp) 14 print(checkcode)
4.shelve模块
1 import shelve 2 3 d = shelve.open('shelve_test') 4 5 def stu_data(name,age): 6 print("register stu",name,age) 7 name = ["hsc","***","abm"] 8 info = {"name":"hsc","age":18} 9 10 d["test"] = name 11 d["info"] = info 12 d['func'] = stu_data
5.shutil模块
1 import shutil 2 3 # f1 = open("random mod .py") 4 # f2 = open("random_new .py",'w') 5 # shutil.copyfileobj(f1,f2) 6 7 # shutil.copyfile("笔记",r'd:\123') 8 shutil.make_archive('www','gztar',root_dir=r'C:\Users\heshaochuan\PycharmProjects\py_s15\day6')
6. logging模块
1 import logging 2 3 log_test = logging.getLogger('TEST') 4 logging.basicConfig(filename='wwwwwwwwww.log',level=logging.INFO) 5 logging.debug('mthgh') 6 logging.info('2131234324')
7.re模块
常用正则表达式符号
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
'.' 默认匹配除\n之外的任意一个字符,若指定flag DOTALL,则匹配任意字符,包括换行 '^' 匹配字符开头,若指定flags MULTILINE,这种也可以匹配上(r "^a" , "\nabc\neee" ,flags = re.MULTILINE) '$' 匹配字符结尾,或e.search( "foo$" , "bfoo\nsdfsf" ,flags = re.MULTILINE).group()也可以 '*' 匹配 * 号前的字符 0 次或多次,re.findall( "ab*" , "cabb3abcbbac" ) 结果为[ 'abb' , 'ab' , 'a' ] '+' 匹配前一个字符 1 次或多次,re.findall( "ab+" , "ab+cd+abb+bba" ) 结果[ 'ab' , 'abb' ] '?' 匹配前一个字符 1 次或 0 次 '{m}' 匹配前一个字符m次 '{n,m}' 匹配前一个字符n到m次,re.findall( "ab{1,3}" , "abb abc abbcbbb" ) 结果 'abb' , 'ab' , 'abb' ] '|' 匹配|左或|右的字符,re.search( "abc|ABC" , "ABCBabcCD" ).group() 结果 'ABC' '(...)' 分组匹配,re.search( "(abc){2}a(123|456)c" , "abcabca456c" ).group() 结果 abcabca456c '\A' 只从字符开头匹配,re.search( "\Aabc" , "alexabc" ) 是匹配不到的 '\Z' 匹配字符结尾,同$ '\d' 匹配数字 0 - 9 '\D' 匹配非数字 '\w' 匹配[A - Za - z0 - 9 ] '\W' 匹配非[A - Za - z0 - 9 ] 's' 匹配空白字符、\t、\n、\r , re.search( "\s+" , "ab\tc1\n3" ).group() 结果 '\t' '(?P<name>...)' 分组匹配 re.search( "(?P<province>[0-9]{4})(?P<city>[0-9]{2})(?P<birthday>[0-9]{4})" , "371481199306143242" ).groupdict( "city" ) 结果{ 'province' : '3714' , 'city' : '81' , 'birthday' : '1993' } |