模块讲解---time模块,datetime模块,random模块,hashlib模块和hmac模块,typing模块,requests模块,re模块
目录
1. 包
2. time模块
1. 优先掌握
2. 了解
3. datetime模块
1. 优先掌握
4. random模块
1. 优先掌握
2. 了解
5. hashlib模块和hmac模块
6. typing模块
7. requests模块
8. re模块
1. re模块的正则表达式的元字符和语法
2. 贪婪模式和非贪婪模式
3. 匹配邮箱实例
4. re模块中的常用功能函数
5. 修饰符 re.S
6. 补充
目录
- \1. 包
- 2. 了解
- \3. datetime模块
- \4. random模块
- 5. hashlib模块和hmac模块
- 6. typing模块
- 7. requests模块
- \8. re模块
1. 包
什么是包
包就是模块
包有什么用
- 当模块内的函数过多时,为了方便管理函数,把多个函数划分成多个模块,但同时不同改变原来的导入方式,把多个模块放入一个包(文件夹)内。
- 通过包,可以不改变用户的导入方式(就和装饰器不改变被装饰函数的调用方式同理),提高用户体验
- 未来导包就是导
__init__
在包的用法中主要注意的有两点
- 包是含有
__init__.py的文件夹;导包就是导入__init__ - 包一定是被当做模块文件导入,模块文件的搜索路径以执行文件为准
- 包是含有
相对导入和绝对导入(只能在包中内部使用)
相对导入:
.表示(同一文件夹下的)当前文件的目录..表示当前文件的父目录...表示当前文件的爷爷目录
绝对导入:
- 就是不用点表示目录,直接写名字
2. time模块
time模块的作用
提供了三种不同类型的时间(时间戳),三种不同类型的时间可以相互转换
三种类型的时间
- 格式化时间
- 结构化时间
- 时间戳
他们是以结构化时间为中间介质,格式化时间和结构化时间可以互相转化;时间戳可以和结构化时间互相转化
1. 优先掌握
import time
time.time() # 时间戳 (从计算机元年开始到现在的时间,以秒计算显示)
time.sleep(1) # 让程序运行到这一步暂停1秒
2. 了解
import time
print(time.time()) # 时间戳形式
# 格式化时间
print(time.strftime('%Y-%m-%d %X'))
# 结构化时间
print(time.localtime())
# 结构化时间 --》 格式化时间
struct_time = time.localtime(3600*24*365)
print(time.strftime('%Y-%m-%d %X',struct_time))
# 格式化时间 --》 结构化时间
format_time = time.strftime('%Y-%m-%d %X')
print(time.strptime(format_time,'%Y-%m-%d %X'))
# 结构化时间 --》 时间戳
struct_time = time.localtime(3600*24*365)
print(time.mktime(struct_time))
# 时间戳 --》 结构化时间
time_stamp = time.time()
print(time.localtime(time_stamp))
3. datetime模块
datetime模块的作用
可以实现时间的加减
1. 优先掌握
import datetime
# 获取当前时间
now = datetime.datetime.now()
print(now) # 2019-09-28 19:56:44.330734
# 默认3天 # 2019-10-01 19:56:44.330734
print(now + datetime.timedelta(3))
# 加3周 #2019-10-19 19:56:44.330734
print(now + datetime.timedelta(weeks=3))
# 加3小时 2019-09-28 22:56:44.330734
print(now + datetime.timedelta(hours=3))
# 减3小时 # 2019-09-28 16:56:44.330734
print(now - datetime.timedelta(hours=3))
print(now + datetime.timedelta(hours=-3))
# 1949-10-01 10:01:00
print(now.replace(year=1949, month=10, day=1, hour=10, minute=1, second=0, microsecond=0))
4. random模块
random模块的作用
产生随机数
1. 优先掌握
import random
# 掌握
# 0-1
print(random.random())
# 产生一个[1-3]之间包括首尾的随机数
print(random.randint(1,3))
# 打乱
lt=[1,2,3]
random.shuffle(lt)
print(lt)
# 随机选择一个
print(random.choice(lt))
# 只随机一次 --> 梅森旋转算法
import time
# random.seed(time.time())
# random.seed(111111111111)
seed() 方法是改变随机数生成器的种子,也就是说,当使用seed()方法后,后面的产生的随机数就是一样的了。
seed()括号内的数不同,产生的随机数种子也不同
就是说 例如:
random.seed(1) 后面再产生的随机数都为 2
random.seed(2)后面再产生的随机数都为 3
print(random.random())
2. 了解
# 了解
print(random.sample([1,'a','c',2,3,4],2))
5. hashlib模块和hmac模块
hashlib模块的作用
对字符加密
hmac模块的作用
对字符加密,并且加上密钥,相当于用了两层加密。
hashlib模块的实例
import hashlib
# 叠加性
m = hashlib.md5()
# m.update(b'say')
# m.update(b'hello') # 981fe96ed23ad8b9554cfeea38cd334a
m.update(b'hash123456')
print(m.hexdigest()) # 对于不同的字符而言,用不重复
# 981fe96ed23ad8b9554cfeea38cd334a
# 手机号/生日/性别/qq账号/以前的密码/ --》 挖矿(算法)
# 1 2 3 5 71113 111111111111111 - 1111111111111111111111 111111111111111111111111111111111111111111111111111
hash_pwd = '0562b36c3c5a3925dbe3c4d32a4f2ba2'
pwd_list = [
'hash3714',
'hash1313',
'hash94139413',
'hash123456',
'123456hash',
'h123ash',
]
for pwd in pwd_list:
m = hashlib.md5()
m.update(pwd.encode('utf8'))
res = m.hexdigest()
if res == hash_pwd:
print(f'获取密码成功:{pwd}')
hamc模块的实例
import hmac m = hmac.new(b'maerzi')
m.update(b'hash123456') # f82317e44545b0ab087109454814b5c4
print(m.hexdigest()) m = hmac.new(b'sdfjhjk2394879ul%$$Y#($&')
m.update(b'hash123456') # 2a70fd0f13cb49357f40d326a4e071a2
print(m.hexdigest()) pwd_list = [
'hash3714',
'hash1313',
'hash94139413',
'hash123456',
'123456hash',
'h123ash',
]
6. typing模块
typing模块的作用
与函数联用,控制函数参数的数据类型,提供了基础数据类型之外的数据类型(如
Iterable, Iterator, Generator)实例
def func(x: int, lt: Iterable) -> list:
return [1, 2, 3] func(10, '123123')
7. requests模块
request是模块的作用
爬数据的模块,模拟浏览器对url发送请求,获取数据
实例
# url ——> 一个特定的网址 -》 永不重复
import requests response = requests.get('https://ishuo.cn')
data = response.text
print(data)
re模块
1、\w \W
print(re.findall('\w','ab 12\+- _*&')) #\w 匹配字母 数字 及下划线
执行结果:['a', 'b', '1', '2', '_']
print(re.findall('\W','ab 12\+- _*&')) #\W 匹配非字母 数字 及下划线
执行结果:[' ', '\\', '+', '-', ' ', '*', '&']
2、\s \S
print(re.findall('\s','ab 12\+- _*&')) #\s 匹配任意空白字符,等价于[\t\n\r\f]
执行结果:[' ', ' ']
print(re.findall('\S','ab 12\+- _*&')) #\S 匹配非空白字符
执行结果:['a', 'b', '1', '2', '\\', '+', '-', '_', '*', '&']
3、\d \D
print(re.findall('\d','ab 12\+- _*&')) #\d 匹配任意数字,等价于[0-9]
执行结果:['1', '2']
print(re.findall('\D','ab 12\+- _*&')) #\D 匹配非数字
执行结果:['a', 'b', ' ', '\\', '+', '-', ' ', '_', '*', '&']
综合:
print(re.findall('\w_sb','egon alex_sb12332wxx_sb,lxx_sb'))
执行结果:['x_sb', 'x_sb', 'x_sb']
4、\A 基本上不用
print(re.findall('\Aalex','alex isalex sb'))#从头开始匹配只匹配第一个alex
执行结果:['alex']
print(re.findall('alex','alex isalex sb'))
执行结果:['alex', 'alex']
5、^
print(re.findall('^alex','alex is salexb'))#从头开始匹配,匹配到第一个则不往后匹配
执行结果:['alex']
print(re.findall('sb','alexsb is sbalexsb'))#从头开始匹配,匹配所有
执行结果:['sb', 'sb', 'sb']
print(re.findall('^sb','alexsb is sbalexsb'))#从头开始匹配,第一个没有则不往后面匹配
执行结果:[]
6、\Z $
print(re.findall('sb\Z','alexsb is sbalexsb'))#从尾部开始匹配,匹配到则不往前匹配
执行结果:['sb']
print(re.findall('sb$','alexsb is sbalexsb'))#从尾部开始匹配,匹配到则不往前匹配
执行结果:['sb']
综合:
print(re.findall('^ebn$','ebn'))#从头开始找,正反找都是ebn,都可以匹配上
执行结果:['ebn']
7、\n \t (同理)
print(re.findall('\n','a\nc a\tc al\nc'))#匹配到\n
执行结果:['\n', '\n']
print(re.findall('a\nc','a\nc a\tc al\nc'))#匹配到['a\nc']
执行结果:['a\nc']
重复匹配: . ? + {m,n} . .*?
1、. :代表除了换行符外的任意一个字符
print(re.findall('a.c','abc alc aAsc aaaaaac'))#匹配以a开头以c结尾.代表中间的任意一个字符
执行结果:['abc', 'alc', 'aac']
print(re.findall('a.c','abc alc aAc aaaaaa\nc'))#ac中间有换行符\n所以匹配不到a\nc
执行结果:['abc', 'alc', 'aAc']
print(re.findall('a.c','abc alc aAsc aaaaaa\nc',re.DOTALL))#.能匹配ac中间的所有一个字符,包括\n
执行结果:['abc', 'alc', 'a\nc']
2、? :代表左边那一个字符重复0次或1次
print(re.findall('ab?','a ab abb abbb abbbb abbbbb albbbbb'))#从头匹配ab中,b是零个或一个
执行结果:['a', 'ab', 'ab', 'ab', 'ab', 'ab', 'a']
3、* :代表左边那一个字符出现0次或无穷次
print(re.findall('ab*','a ab abb abbb abbbb abbbbb albbbbbbb'))#从头匹配ab中,b是零个或无穷个
执行结果:['a', 'ab', 'abb', 'abbb', 'abbbb', 'abbbbb', 'a']
4、+ :代表左边那一个字符出现至少一次或无穷次
print(re.findall('ab+','a ab abb abbb abbbb abbbbb albbbbbbb'))#从头匹配ab中,b是一个或无穷个
执行结果:['ab', 'abb', 'abbb', 'abbbb', 'abbbbb']
5、{m,n} :代表左边那一个字符出现m次到n次
print(re.findall('ab?','a ab abb abbb abbbb abbbbb albbbbbb'))
print(re.findall('ab{0,1}','a ab abb abbb abbbb abbbbb albbbbbb'))
执行结果:['a', 'ab', 'ab', 'ab', 'ab', 'ab', 'a'] , ['a', 'ab', 'ab', 'ab', 'ab', 'ab', 'a']
print(re.findall('ab*','a ab abb abbb abbbb abbbbb albbbbbbb'))
print(re.findall('ab{0,}','a ab abb abbb abbbb abbbbb albbbbbbb'))
执行结果:['a', 'ab', 'abb', 'abbb', 'abbbb', 'abbbbb', 'a'] , ['a', 'ab', 'abb', 'abbb', 'abbbb', 'abbbbb', 'a']
print(re.findall('ab+','a ab abb abbb abbbb abbbbb albbbbbbb'))
print(re.findall('ab{1,}','a ab abb abbb abbbb abbbbb albbbbbbb'))
执行结果:['ab', 'abb', 'abbb', 'abbbb', 'abbbbb'] , ['ab', 'abb', 'abbb', 'abbbb', 'abbbbb']
print(re.findall('ab{1,3}','a ab abb abbb abbbb abbbbb albbbbbbb'))
执行结果:['ab', 'abb', 'abbb', 'abbb', 'abbb']
6、.* :匹配任意长度,任意的字符=====》贪婪匹配
print(re.findall('a.*c','ac a123c aaaac a * 123) ()c asdfsdfkjdls'))#尽可能长的匹配
执行结果:['ac a123c aaaac a * 123) ()c']
7、.*? :非贪婪匹配
print(re.findall('a.*?c','a123c456c'))#尽可能短的匹配
执行结果:['a123c']
8、() :分组
print(re.findall('(alex)_sb','alex_sb sfksdfksdalex_sb'))#在匹配到的情况下只留括号内的内容
执行结果:['alex', 'alex']
例子:非贪婪匹配到网址
print(re.findall('href="(.*?)"','<li><a id="blog_nav_sitehome" class="menu" href="https://www.cnblogs.com/happyfei/">博客园</a></li>'))
执行结果:['https://www.cnblogs.com/happyfei/']
9、[] :匹配一个指定范围内的字符(这一字符来自于括号内定义的)
print(re.findall('a[0-9]c','a1c a+c a2c a9c a*c a11c a-c acc aAc '))#-号在[]内有特殊意义,如果要匹配带-号的,-号要放在最前面或最后面
执行结果:['a1c', 'a2c', 'a9c']
print(re.findall('a[-+*]c','a1c a+c a2c a9c a*c a11c a-c acc aAc '))
执行结果:['a+c', 'a*c', 'a-c']
print(re.findall('a[a-zA-Z]c','a1c a+c a2c a9c a*c a11c a-c acc aAc '))
执行结果:['acc', 'aAc']
print(re.findall('a[^a-zA-Z]c','a c a1c a+c a2c a9c a*c a11c a-c acc aAc '))#[]内的^代表取反的意思
执行结果:['a c', 'a1c', 'a+c', 'a2c', 'a9c', 'a*c', 'a-c']
例子:取出_sb
print(re.findall('[a-z]_sb','egon alex_sb12332wxx_sb,lxx_sb'))#[]匹配一个字符后面跟_sb
执行结果:['x_sb', 'x_sb', 'x_sb']
print(re.findall('[a-z]+_sb','egon alex_sb12332wxxxxx_sb,lxx_sb'))#[]+匹配多个字符后面跟_sb
执行结果:['alex_sb', 'wxxxxx_sb', 'lxx_sb']
print(re.findall('([a-z]+)_sb','egon alex_sb12332wxxxxx_sb,lxx_sb'))#只取到_sb的人名
执行结果:['alex', 'wxxxxx', 'lxx']
10、| :代表或者
print(re.findall('compan(ies|y)','Too many companies have gone bankrupt, and the next none is my company'))#取出公司的英文单词
执行结果:['ies', 'y']
注:(?:代表取匹配成功的所有内容,而不仅仅只是括号内的内容)
print(re.findall('compan(?:ies|y)','Too many companies have gone bankrupt, and the next none is my company'))
执行结果:['companies', 'company']
print(re.findall('alex|sb','alex sb ssdfsdf alex sb egon'))
执行结果:['alex', 'sb', 'alex', 'sb']
11、re模块的其他用法
1 print(re.findall('alex|sb','123123 alex sb sdlfjlsdkegon alex sb egon'))
2 print(re.search('alex|sb','123123 alex sb sdlfjlsdkegon alex sb egon').group())
3 #执行结果:['alex', 'sb', 'alex', 'sb'] , alex
4
5
6 print(re.search('^alex','alex sb sdlfjlsdkegon alex sb egon').group())#表示从头开始匹配
7 print(re.match('alex','alex sb sdlfjlsdkegon alex sb egon').group())#表示从头开始匹配
8 #执行结果:alex , alex
9
10
11 info='a:b:c:d'
12 print(info.split(':'))
13 print(re.split(':',info))
14 #执行结果:['a', 'b', 'c', 'd'] , ['a', 'b', 'c', 'd']
15
16 info='a :c\d/e'
17 print(re.split('[ :\\\/]',info))
18 #执行结果:['a', '', 'c', 'd', 'e']
19
20 #需求:xxx与Sb调换
21 print(re.sub('(xxx)(.*?)(SB)',r'\3\2\1',r'xxx is SB'))
22 #执行结果:SB is xxx
23
24 print(re.sub('([a-zA-Z]+)([^a-zA-Z]+)([a-zA-Z]+)([^a-zA-Z]+)([a-zA-Z]+)',r'\5\2\3\4\1',r'xxx123+ is SB'))
25 #执行结果:SB123+ is xxx
26
27 pattern=re.compile('alex')#把常用的正则表达式式存起来,以后直接用
28 print(pattern.findall('alex is alex sdjflk alexalex'))
29 #执行结果:['alex', 'alex', 'alex', 'alex']
模块讲解---time模块,datetime模块,random模块,hashlib模块和hmac模块,typing模块,requests模块,re模块的更多相关文章
- 包 ,模块(time、datetime、random、hashlib、typing、requests、re)
目录 1. 包 1. 优先掌握 2. 了解 3. datetime模块 1. 优先掌握 4. random模块 1. 优先掌握 2. 了解 5. hashlib模块和hmac模块 6. typing模 ...
- Python3基础(5)常用模块:time、datetime、random、os、sys、shutil、shelve、xml处理、ConfigParser、hashlib、re
---------------个人学习笔记--------------- ----------------本文作者吴疆-------------- ------点击此处链接至博客园原文------ 1 ...
- 包及常用模块(time、datetime、random、sys)
什么是包?‘ #官网解释 Packages are a way of structuring Python’s module namespace by using “dotted module nam ...
- day16——自定义模块、time、datetime、random
day16 自定义模块 自定义一个模块 import :导入(拿工具箱) # import test # test.func() 导入发生的事情 在当前的名称空间中开辟一个新的空间 将模块中所有的代码 ...
- Python 第五篇(上):算法、自定义模块、系统标准模块(time 、datetime 、random 、OS 、sys 、hashlib 、json和pickle)
一:算法回顾: 冒泡算法,也叫冒泡排序,其特点如下: 1.比较相邻的元素.如果第一个比第二个大,就交换他们两个. 2.对每一对相邻元素作同样的工作,从开始第一对到结尾的最后一对.在这一点,最后的元素应 ...
- Python模块:time、datetime、random、os、sys、optparse
time模块的方法: 时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量. struct_time时间元组,共有九个元素组.如下图: time.localtime([secs]): ...
- Python 常用方法和模块的使用(time & datetime & os &random &sys &shutil)-(六)
1 比较常用的一些方法 1.eval()方法:执行字符串表达式,并返回到字符串. 2.序列化:变量从内存中变成可存储或传输到文件或变量的过程,可以保存当时对象的状态,实现其生命周期的延长,并且需要时可 ...
- 学习16内容# 1.自定义模块 # 2.time # 3.datetime # 4.random
模块的定义与分类 模块是什么? 这几天,我们进入模块的学习.在学习模块之前,我们首先要知道,什么是模块? 一个函数封装一个功能,你使用的软件可能就是由n多个函数组成的(先不考虑面向对象).比如 ...
- Python常用模块之json、pickle、random、hashlib、collections
1.json和pickle json用于字符串和Python数据类型间进行转换pickle用于python特有的类型和python的数据类型间进行转换json和pickle均提供了四种方法dumps, ...
- Python常用标准库之datetime、random、hashlib、itertools
库:具有相关功能模块的集合 import sys.builtin_module_names #返回内建模块的名字modules 查看所有可用模块的名字 1.1.1获取当前日期和时间 from date ...
随机推荐
- Sql server 中count(1) 与 sum(1) 那个更快?
上一篇中,简单的说明了下 count() 与 sum() 的区别,虽然count 函数是汇总行数的,不过我汇总行数的时候经常是使用SUM(1) ,那么问题来了,count(1) 与 sum(1) 那 ...
- Redis部分
- C++视频读取与视频保存
VideoCapture cap("E:\\122.avi"); //计算视频帧数 int VedioFPS = cap.get(CV_CAP_PROP_FPS); //cout ...
- 数据类型 _python
字符串 str # a ="asd bfg" # print(a.capitalize()) #首字母大写 # print(a.title()) #每个单词首字母大写 # prin ...
- matplotlib笔记2
颜色和样式 八种内建默认颜色缩写b:blue g:green r:red c:cyan m:magenta y:yellow k:black w:white其它颜色表示方法可以参照百度给的值https ...
- Geometers Anonymous Club CodeForces - 1195F (闵可夫斯基和)
大意: 给定$n$个凸多边形, $q$个询问, 求$[l,r]$内闵可夫斯基区间和的顶点数. 要用到一个结论, 闵可夫斯基和凸包上的点等于向量种类数. #include <iostream> ...
- Luogu5284 十二省联考2019字符串问题(后缀树+拓扑排序)
对反串建SAM弄出后缀树,每个b串通过倍增定位其在后缀树上对应的节点,根据其长度将节点拆开.然后每个a串也找到对应的节点,由该节点向表示a串的节点连边,再把所给的边连上跑拓扑排序即可. #includ ...
- Mish:一个新的SOTA激活函数,ReLU的继任者
Mish:一个新的SOTA激活函数,ReLU的继任者 CVer 昨天 以下文章来源于AI公园 ,作者ronghuaiyang AI公园 专注分享干货的AI公众号,图像处理,NLP,深度学习,机器学 ...
- 从业务流程角度:分析TMS系统各个功能模块
TMS的主要功能是协调承运商.运营商.货主三种角色人员分工合作共同完成运输任务,并实现对运输任务的跟踪管理.本文将按照业务流程顺序对TMS系统各个功能模块进行分析说明. 一.业务描述 新零售的兴起及& ...
- (七)Activiti之历史活动查询和历史任务查询和流程状态查询
一.历史活动查询 本章案例是基于上一章节案例的基础上,流程走完后进行测试的,也就是下图的流程从学生请假到班主任审批都已经完成,本章用来测试查询历史活动和历史任务的 activiti5的历史活动包括所有 ...