本节大纲:
1、模块讲解
2、hashlib and hmac
3、random
4、shelve
5、shutil
6、time and datetime
7、os and sys
8、re
9、xml
10、ConfigParser
1、模块讲解
定义:用一砣代码实现了某个功能的代码集合
导入方式:
import module_text
from module import module_text
from module import module_text as MT
from . import module_text
import本质:
导入模块的本质就是把python文件解释一遍
导入包的本质就是去执行/解释包下的__init__.py文件
2、hashlib and hmac(文件加密)
hashlib主要提供SHA1, SHA224, SHA256, SHA384, SHA512 ,MD5 算法,越大保密性越强,但是效率越低
hmac 提供key-value的加密方法
####加密方式不是一行一行加密,而是一起加密
--------------------------------------------
import hashlib
m = hashlib.md5()
m.update(b'hello')
print(m.hexdigest())
m.update(b'world')
等于:
m.update(b'helloworld')
print(m.hexdigest())
m2 = hashlib.sha256
m.update('如果这都不算爱'.encode('utf-8')) #如果需要加密中文,需要encode()
2) hmac
import hmac
h = hmac.new(b'hello')
h.update(b'world')
print(h.hexdigest())
等于:
h = hmac.new(b'hello',b'world')
print(h.hexdigest())
3、random(随机数)
random.random() # 随机出现从0到1之前的数字
random.randrange(x,y) # 随机出现从x到y不包括y之间的整数
random.uniform(x,y) # 随机出现x到y之间的浮点数
random.randint(x,y) # 随机出现x到y之间的整数包括xy
random.choice() #从序列中随机出现里面的值
random.sample(‘序列’,‘count’) # 随机出现序列中count个数的值
random.shuffle() # 打乱序列的数值
----------------------验证码程序------------------
def check_mk(n):
check_code = ''
for i in range(n):
current = random.randrange(n)
if i == current:
tmp = random.randint(0,9)
else:
tmp = chr(random.randint(65,90))
check_code += str(tmp)
return check_code
4、shelve
shelve模块是一个简单的k,v将内存数据通过文件持久化的模块,可以持久化任何pickle可支持的python数据格式
---------------------shelve模块-----------------------
list = [1,2,3,4]
name = 'lili'
age = '22' h = shelve.open('shelve_test1')
h['name'] = name
h['list'] = list
h['age'] = age h.close() c = shelve.open('shelve_test1')
print(c['name'])
5、shutil
高级的 文件、文件夹、压缩包 处理模块
shutil.copyfileobj(f1,f2) #复制文件,需要先open文件
shutil.copyfile(src, dst) # 复制文件,不需要打开文件
shutil.copymode(src, dst) #复制权限
shutil.copy(src, dst) #复制文件和权限
shutil.copy2(src, dst) #复制文件和状态
shutil.copytree(path,path1) # 递归复制目录
shutil.rmtree(path,path1) # 递归删除目录
shutil.make_achieve() # 文件压缩
6、time and datetime
时间模块:time,datetime
1、time
时间获取方式:1、时间戳(timestamp) 2、元组(struct_time) 3、格式化字符串(format string)
2、datetime
(1)、时间加减
time:
time.gmtime() # 返回以元组形式的UTC时间
time.localtime() # 返回以元组形式的当时时间
time.time() # 返回当时时间的时间戳
time.mktime() # 将元组形式的
time.strftime() # 将元组转换成格式化字符串
time.strptime() #将格式化字符串转换成元组
time.asctime() # 将元组转换成固定格式的字符串
time.ctime() #将时间戳转换成固定格式的字符串
- %y 两位数的年份表示(00-99)
- %Y 四位数的年份表示(000-9999)
- %m 月份(01-12)
- %d 月内中的一天(0-31)
- %H 24小时制小时数(0-23)
- %I 12小时制小时数(01-12)
- %M 分钟数(00=59)
- %S 秒(00-59)
- %a 本地简化星期名称
- %A 本地完整星期名称
- %b 本地简化的月份名称
- %B 本地完整的月份名称
- %c 本地相应的日期表示和时间表示
- %j 年内的一天(001-366)
- %p 本地A.M.或P.M.的等价符
- %U 一年中的星期数(00-53)星期天为星期的开始
- %w 星期(0-6),星期天为星期的开始
- %W 一年中的星期数(00-53)星期一为星期的开始
- %x 本地相应的日期表示
- %X 本地相应的时间表示
- %Z 当前时区的名称
- %% %号本身
datetime:时间加减
print(datetime.datetime.now()) #以字符串方式打印当前时间
print(datetime.datetime.now() + datetime.timedelta(3)) #打印三天后的时间
print(datetime.datetime.now() + datetime.timedelta(hours = -3)) # 打印三小时前的时间
print(datetime.datetime.now() + datetime.timedelta(minutes = 30)) # 打印30分钟后的时间
7、os and sys
os:
os.getcwd() # 获取当前目录路径
os.chdir() # 改变当前路径
print(os.curdir) # 相对路径 ‘.’
print(os.pardir) # 相对上级路径 ‘..’
os.makedirs(路径)#递归创建目录
os.removedirs(路径)#递归删除空目录
os.mkdir(路径) # 创建目录,如果上一级不存在,则报错
os.rmdir(路径) # 删除空文件
os.listdir(路径) # 显示当前目录下的内容
os.stat(路径) # 获取目录\文件信息
os.sep # 输出当前系统的路径分隔符
os.linesep # 输出当前系统的换行符
os.pathsep # 输出当前系统的不同路径的分隔符
os.environ # 输出当前的环境变量
os.path.split() # 分割目录路径与文件
os.path.bathname() # 输出路径最底层名字
os.path.exists() # 判断路径是否存在
os.path.isabs() # 判断是否为绝对路径
os.path.isfile() # 判断是否为文件
os.path.isdir() # 判断是否为目录
os.path.join(path1,path2.....) # 将多个路径组合返回,第一个绝对路径之前的参数将被忽略,路径可不存在
os.path.getatime() # 返回文件或目录存取时间
os.path.getmtime() # 返回文件或目录修改时间
os.path.getctime() # 返回文件或目录创建时间
8、re正则表达式
re.match 从头开始匹配 ^括号在match里面没用
re.search 匹配包含,从左往右,用.group返回结果
re.findall 把所有匹配到的所有字符放到以列表中的元素返回
re.splitall 把匹配到的字符当做列表分隔符
re.sub 匹配字符并替换 匹配结束后可以加匹配模式
re.I
re.M
re.S '.' 表示匹配任意一个字符
'+' 匹配一个或多个字符
'$' 匹配字符结尾
'^' 匹配字符开头
'?' 匹配前一个字符0次或一次
'{m}' 匹配前一个字符m次
'{n,m}' 匹配前一个字符n到m次
'(....)' 分组匹配 \A 同^ 开头
\Z 同$ 结尾
\d 匹配数字
\D 匹配非数字
\w 匹配[A-Za-z0-9]
\W 匹配非[A-Za-z0-9]
\s 匹配空白字符,TAB自动为\t
print(re.findall('a\w+n','awnSsdaBilnlldGg')) = print(re.findall('a[a-zA-Z0-9]+n','awnSsdaBilnlldGg')) # ['awnSsdaBiln']
print(re.findall('a.+u$','duaklu') # [uaklu] $以u结尾 print(re.findall('aaa?','dsaaadiuoaaioa')) # [aaa,aa] 'aaa?' 返回前面那个a零次或者一次 可以匹配aaa或者aa
print(re.findall('[0-9]{3}','duan1nd432io2346k6j')) # 返回3个成组的数字,多于3个则只返回3个
print(re.findall('[0-9]{1,3}','duan1nd432io23423k6j')) # 返回1到3个成组的数字,一个组合多于3个则拆分
print(re.search('abc{3}','abcddfdseccabccc')) # [abccc]
print(re.findall('abc{1,3}','abcddfdseccabccc'))# [abc,abcc,abccc]
print(re.search('abc|ABC','asdabc123ABC')) # 或 search >>> abc
print(re.findall('abc|ABC','asdabc123ABC')) # 或 findall >>> abc , ABC print(re.search('(abc){2}(\|\|=){2}','ASDabcabc||=||=daf')) # 分组匹配 print(re.search('\A[0-9]+[a-z]\Z','123b') # 数字开头,小写字母结尾 高级分组匹配:
print(re.search('(?P<name>[0-9]{6})(?P<birthday>[0-9]{4})(?P<month>[0-9]{2})','500107197012').groupdict())
#将数据传为字典 >>>{'name': '500107', 'birthday': '1970', 'month': '12'}
截断
print(re.search('\W+','dfa#dagad$dg')) #以特殊字符截断 >>>['dfa', 'dagad', 'dg']
替换
print(re.sub('[0-9]+','#','asdf1231adfadf21314')) # 将数字以#替换 >>>[ asdf#adfadf# ]
9、xml模块
例子:xml_text.xml
<data>
<country name="Liechtenstein">
<rank updated="yes">2</rank>
<year updated="yes">2013</year>
<gdppc>141100</gdppc>
<neighbor direction="E" name="Austria" />
<neighbor direction="W" name="Switzerland" />
</country>
<country name="Singapore">
<rank updated="yes">5</rank>
<year updated="yes">2016</year>
<gdppc>59900</gdppc>
<neighbor direction="N" name="Malaysia" />
</country>
<country name="Panama">
<rank updated="yes">69</rank>
<year updated="yes">2016</year>
<gdppc>13600</gdppc>
<neighbor direction="W" name="Costa Rica" />
<neighbor direction="E" name="Colombia" />
</country>
</data>
import xml.etree.ElementTree as ET
tree = ET.parse('xmltext.xml')
root = tree.getroot()
print(root.tag) for child in root:
print(child.tag,child.attrib)
for i in child:
print(i.tag,i.text,i.attrib) for node in root.iter('year'):
print(node.tag,node.text) #修改
for node_1 in root.iter('year'):
new_year = int(node_1.text)+1
node_1.text = str(new_year)
node_1.set('updated','yes')
tree.write('xmltext.xml') #删除 for country in root.findall('country'):
rank = int(country.find('rank').text)
if rank>50:
root.remove(country)
tree.write('output.xml')
10、Configparser
import configparser config = configparser.ConfigParser()
config['DEFAULT'] = {
'ServerAliveInterval':'45',
'Compression':'Yes',
'CompressionLevel':'9'
} config['bitnucket.org'] = {}
config['bitnucket.org']['User'] = 'hg'
config['topsecret.server.com'] = {}
topsecret = config['topsecret.server.com']
topsecret['Hose Port'] = '5022'
topsecret['ForwardX11'] = 'no'
config['DEFAULT']['ForwardX11'] = 'yes'
with open('example.ini','w') as configfile:
config.write(configfile) >>>>>>
[DEFAULT]
serveraliveinterval = 45
compression = Yes
compressionlevel = 9
forwardx11 = yes [bitnucket.org]
user = hg [topsecret.server.com]
hose port = 5022
forwardx11 = no