Python之路,day6-Python基础

时间:2022-03-21 19:25:01

1.config 模块

 import configparser

 conf = configparser.ConfigParser()
conf["DEFAULT"] = {'ServerAliveInterval': '',
'Compression': 'yes',
'CompressionLevel': ''}
conf['wwwwwwwww'] = {}
conf['wwwwwwwww']['user'] = 'baidu'
conf['topsecret.server.com'] = {}
topsecret = conf['topsecret.server.com']
topsecret['Host Port'] = ''
with open('conf.ini','w') as f:
f.write(conf)

2.hashlib操作

 import hmac
h = hmac.new(b'hsc')
h.update(b'')
print(h)

3.random模块

 import random
print(random.random())
print(random.randint(1,2)) #0,1,2 随机
print(random.randrange(1,2))#0,1 随机 checkcode = ''
for i in range(6):
current = random.randrange(0,6)
if current != i:
temp = chr(random.randint(65,90))
else:
temp = random.randint(0,9)
checkcode += str(temp)
print(checkcode)

4.shelve模块

 import shelve

 d = shelve.open('shelve_test')

 def stu_data(name,age):
print("register stu",name,age)
name = ["hsc","xjp","abm"]
info = {"name":"hsc","age":18} d["test"] = name
d["info"] = info
d['func'] = stu_data

5.shutil模块

 import shutil

 # f1 = open("random mod .py")
# f2 = open("random_new .py",'w')
# shutil.copyfileobj(f1,f2) # shutil.copyfile("笔记",r'd:\123')
shutil.make_archive('www','gztar',root_dir=r'C:\Users\heshaochuan\PycharmProjects\py_s15\day6')

6. logging模块

 import logging

 log_test = logging.getLogger('TEST')
logging.basicConfig(filename='wwwwwwwwww.log',level=logging.INFO)
logging.debug('mthgh')
logging.info('')

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'}