包:是模块的一种形式,包的本质就是一个含有.py
的文件的文件夹。
1. 包 == 模块, 包拿来导入用的
2.包是含有__init__.py的文件夹; 导包就是导入__init__
3.包一定是被当作模块文件导入,模块文件 m1.py/m2.py 的搜索路径以执行文件 包的介绍.py 路径为准
绝对导入和相对导入
绝对导入:
# aaa/.py
from aaa.m1 import func1
from aaa.m2 import func2
相对导入:
.代表当前被导入文件所在的文件夹
..代表当前被导入文件所在的文件夹的上一级
...代表当前被导入文件所在的文件夹的上一级的上一级
from .m1 import func1
from .m2 import func2
注意事项
- 包内所有的文件都是被导入使用的,而不是被直接运行的
- 包内部模块之间的导入可以使用绝对导入(以包的根目录为基准)与相对导入(以当前被导入的模块所在的目录为基准),推荐使用相对导入
- 当文件是执行文件时,无法在该文件内用相对导入的语法,只有在文件时被当作模块导入时,该文件内才能使用相对导入的语法
- 凡是在导入时带点的,点的左边都必须是一个包,
import aaa.bbb.m3.f3
错误
time 模块
提供了三种不同类型的时间(时间戳),三种不同类型的时间可以相互转换
import time print(time.time()) # 时间戳形式 打印结果:1569666375.2903588
# 格式化时间 print(time.strftime('%Y-%m-%d %X')) 打印结果:2019-09-28 18:26:15
# 结构化时间 print(time.localtime()) 打印结果:time.struct_time(tm_year=2019, tm_mon=9, tm_mday=28, tm_hour=18, tm_min=26, tm_sec=15, tm_wday=5, tm_yday=271, tm_isdst=0) # 结构化时间 --》 格式化时间 struct_time = time.localtime(3600*24*365) print(4,time.strftime('%Y-%m-%d %X',struct_time)) 打印结果:1971-01-01 08:00:00 # 格式化时间 --》 结构化时间 format_time = time.strftime('%Y-%m-%d %X') print(5,time.strptime(format_time,'%Y-%m-%d %X')) 打印结果:time.struct_time(tm_year=2019, tm_mon=9, tm_mday=28, tm_hour=18, tm_min=26, tm_sec=15, tm_wday=5, tm_yday=271, tm_isdst=-1) # 结构化时间 --》 时间戳 struct_time = time.localtime(3600*24*365) print(6,time.mktime(struct_time)) 打印结果:31536000.0
# 时间戳 --》 结构化时间 time_stamp = time.time() print(7,time.localtime(time_stamp)) 打印结果:time.struct_time(tm_year=2019, tm_mon=9, tm_mday=28, tm_hour=18, tm_min=26, tm_sec=15, tm_wday=5, tm_yday=271, tm_isdst=0) # (*******) time.time() time.sleep(1)
datetime 模块 时间的加减
import datetime now = datetime.datetime.now() print(now) #默认参数是day= print(now + datetime.timedelta(3)) # 加3周 print(now + datetime.timedelta(weeks=3)) # 加3小时 print(now + datetime.timedelta(hours=3)) # 减3小时 print(now - datetime.timedelta(hours=3)) print(now + datetime.timedelta(hours=-3)) print(now.replace(year=1949, month=10, day=1, hour=10, minute=1, second=0, microsecond=0)) 打印结果为: 2019-09-28 18:31:20.382882 2019-10-01 18:31:20.382882 2019-10-19 18:31:20.382882 2019-09-28 21:31:20.382882 2019-09-28 15:31:20.382882 2019-09-28 15:31:20.382882 1949-10-01 10:01:00
random模块
import random # 0-1 print(random.random()) # 0.18743501612833613 # [1-3] print(random.randint(1,3)) # 1 # 打乱 lt=[1,2,3] random.shuffle(lt) print(lt) # [2, 1, 3] # 随机选择一个 print(random.choice(lt)) # 1 # 只随机一次 --> 梅森旋转算法 import time #random.seed(time.time()) # random.seed(111111111111) #固定不变了 print(random.random()) # 0.6613971174425596 # 了解 print(random.sample([1,'a','c',2,3,4],2))从列表中随机取2个数 #[3, 4]
hashlib模块和hmac模块
原字符要加 b'原字符'
hashlib模块:对字符加密
hmac模块:对字符加密,并且加上密钥
import hashlib # 叠加性 m = hashlib.md5() #m.update(b'say') #m.update(b'hello') #与b'sayhello'一样 m.update(b'sayhello') print(m.hexdigest())
import hmac m = hmac.new(b'maerzi')
m.update(b'hash123456') # f82317e44545b0ab087109454814b5c4
print(m.hexdigest())
tyring 模块
与函数联用,控制函数参数的数据类型,提供了基础数据类型之外的数据类型
requests 模块
爬数据,模拟浏览器对url发送请求,拿到数据
import requests response = requests.get('https://ishuo.cn') data = response.text print(data)
re正则
import re # 元字符 s = 'abcdabc' # abc # abc # bc bc # ^:以...开头 res = re.findall('^ab', s) print(res) ##['ab'] res = re.findall('^bc', s) print(res) ##[] # $: 以..结尾 s = 'abcdabc' res = re.findall('bc$', s) print(res) ##['bc'] # .: 任意字符 s = 'abc红abc' res = re.findall('abc.', s) print(res) ##['abc红'] # d: 数字 s = 'skld2342kjk' res = re.findall('d', s) print(res) ## ['2', '3', '4', '2'] # w: 非空,数字字母下划线 s = 'skld_23 42ljk' res = re.findall('w', s) print(res) ##['s', 'k', 'l', 'd', '_', '2', '3', '4', '2', 'l', 'j', 'k'] # s:空,空格/ / s = 'skld_23 42ljk' res = re.findall('s', s) print(res) ## [' '] # D: 非数字 s = 'skld2342ljk' res = re.findall('D', s) print(res) ##['s', 'k', 'l', 'd', 'l', 'j', 'k'] # W: 空 s = 'skld_23 42ljk' res = re.findall('W', s) print(res) ##[' '] # S:非空 s = 'skld_23 42ljk' res = re.findall('S', s) print(res) ##['s', 'k', 'l', 'd', '_', '2', '3', '4', '2', 'l', 'j', 'k'] # +: 前面的一个字符至少1个 s = 'abcddddd abcd abc' print(re.findall('abcd+', s)) ##['abcddddd', 'abcd'] # ?:前面的一个字符0-1个 s = 'abcddddd abcd abc' print(re.findall('abcd?', s)) ## ['abcd', 'abcd', 'abc'] # *:前面的一个字符至少0个 s = 'abcdddddddddddddddddd abcd abc' print(re.findall('abcd*', s)) ## ['abcdddddddddddddddddd', 'abcd', 'abc'] # []: 中括号内的都可以,但列表里面的只能符合一个 s = 'abc bbc cbc dbc' print(re.findall('[abc]bc', s)) ##['abc', 'bbc', 'cbc'] # [^]: 中括号的都不可以 s = 'abc bbc cbc dbc' print(re.findall('[^abc]bc', s)) ## ['dbc'] # |:或 s = 'abc bbc dbc' print(re.findall('abc|bbc', s)) ## ['abc', 'bbc'] # {2}:前面的字符2个 s = 'abccabc abccc' print(re.findall('abc{2}', s)) ## ['abcc', 'abcc'] # {1,2}:前面的字符1到2个 s = 'abccabc abccc' print(re.findall('abc{1,2}', s)) ##['abcc', 'abc', 'abcc'] # 贪婪模式 # .(任意字符)*(0-无穷个) s = 'abcdefgbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbg' print(re.findall('a.*g', s)) 表示a g中间有无数个字符都可以匹配到 ##['abcdefgbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbg'] # 非贪婪模式(*******) # .(任意字符)*(0-无穷个)?(让他进入非贪婪模式) s = 'abcdefgbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbg' print(re.findall('a.*?g', s)) 只要是a g就停止 ## ['abcdefg'] # bug # .(任意字符)*(0-无穷个)?(让他进入非贪婪模式) s = 'abcdefg' print(re.findall('.*?', s)) ## ['', '', '', '', '', '', '', ''] # 了解:特殊构造 # a(?=d) :a后面是数字,但是不要数字,不消耗字符串内容 s = 'a123 aaaa a234 abc' # a1 aa # aa # aa a2 ab print(re.findall('a(?=d)', s)) ##['a', 'a'] print(re.findall('a(?=w)', s)) ##['a', 'a', 'a', 'a', 'a', 'a']
# 匹配邮箱: s = '#@#@#@nickchen121@163.com$$$$////nick@qq.com$$#$#$[]]2287273393@162.com@$2423423lksdlfj#' print(re.findall('w+@w+.com', s)) 打印结果为:['nickchen121@163.com', 'nick@qq.com', '2287273393@162.com']
## compile s = 'abcd abcddd abc' # res = re.compile('abcd*') email_pattern = re.compile('w+@w+.com') phone_patter = re.compile('d{13}') print(re.findall(email_pattern, s))
#match: 从开头找一个,找得到就不找了 ;找不到报错 --》 s = 'ab abcddd abc' res = re.match('abcd*', s) print(res.group())
# search: 从字符串找一个,就不找了 s = 'ab abcddd abc' res = re.search('abcd*', s) print(res.group()) 打印结果为:abcddd
s = 'ab23423abcddd234234abcasdfjlasjdk234l23lk4j2kl34kl25k3j2kl3j5lkj' print(re.split('d+', s)) #以数字为标准,打断字符串 打印结果为: ['ab', 'abcddd', 'abcasdfjlasjdk', 'l', 'lk', 'j', 'kl', 'kl', 'k', 'j', 'kl', 'j', 'lkj']
## sub == replace s = 'ab23423abcddd234234abcasdfjlasjdk234l23lk4j2kl34kl25k3j2kl3j5lkj' print(re.sub('d+', ' ', s)) 打印结果为: ab abcddd abcasdfjlasjdk l lk j kl kl k j kl j lkj
## subn --> 替换了多少次 s = 'ab23423abcddd234234abcasdfjlasjdk234l23lk4j2kl34kl25k3j2kl3j5lkj' print(re.subn('d+', ' ', s)) 打印结果为:('ab abcddd abcasdfjlasjdk l lk j kl kl k j kl j lkj', 12)
## 修饰符 --> re.S会让.匹配换行符(*****) s = '''abc abcabc*abc ''' # .不匹配换行 print(re.findall('abc.abc', s)) # ['abc*abc'] print(re.findall('abc.abc', s, re.S)) # ['abc abc', 'abc*abc']
打印结果为:
['abc*abc']
['abc abc', 'abc*abc']
## 分组 --> 只要括号里的(*****) s = 'abc abcd abcdd' print(re.findall('a(.)c(d)', s)) 打印结果为:[('b', 'd'), ('b', 'd')]
## 有名分组(了解) s = 'abc abcd abcdd' print(re.search('a(?P<name>.)c(?P<name2>d)', s).groupdict()) 打印结果为: {'name': 'b', 'name2': 'd'}
# 超高级用法 s = 'abc123abc123' # c123a print(re.sub('c(d+)a', ' ', s)) print(re.sub('c(?P<name1>d+)a', ' g<name1> ', s)) # g<name1>这个东西不能替换掉 打印结果为: ab bc123 ab 123 bc123