time datetime random hashlib hmac requests re模块

包的作用

当模块内部函数过多时,为了方便管理模块,把一个模块划分成多个模块,但是又不能改变导入方式,把多个模块放入一个包(文件夹)内,未来导包就是到init

1.包就是模块,包是拿来导入用的

2.包是含有_inin_.py的文件夹,导包就是导入_init

3.包一定是被当作模块文件导入,模块文件的搜索路径以执行文件的路径为准

相对导入绝对导入:只能在包中内部使用

#bao
	#init.py
    from bao.m1 import * #from .m1 import *
    from bao.m2 import * #from .m2 import *
    #m1.py
    def f1():
        print("f1")
    def f2():
        print('f2')
    #m2.py
    def f3():
        print("f3")
    def f4():
        print('f4')
#包的介绍.py
from bao import *
f1()
f2()
f3()
f4()

time模块

作用

提供了三种不同类型的时间(时间戳)、三种不同类型的时间可以相互转换

时间戳形式

print(time.time())

格式化时间

print(time.strftime('%Y-%m-%d %X'))

结构化时间

print(time.locatime())

结构化时间—》格式化时间

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

结构化时间—》时间戳

struvt_time=time.localtime(3600*24*365)
print(time.mktime(struct_time)

时间戳—》结构化时间

time_stamp=time.time()
print(time.localtime(time_stamp))

datetime模块

作用

时间加减

现在时间

import datetime
now=datetime.datetime.now()
print(now)

加减

#默认3天
print(now+datetime.timedelta(3))
#加三周
print(now+datetime.timedelta(weeks=3))
#加三小时
print(now+datetime.timedelta(hours=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))

random模块

作用

随机数

掌握

print(random.random())#0-1随机数
print(random.randint(1,3))#【1-3】随机数

# 打乱
lt=[1,2,3]
random.shuffle(lt)
print(lt)

# 随机选择一个
print(random.choice(lt))

#只随机一次--》梅森旋转算法
import time
#random.seed(time.time())#原理类似于默认值而不是时间的变化
random.seed(0)

了解

print(random.sample([1,2,3,'a','b','c',4],2))# 列表随机两个数组合

hashlib模块

作用

对字符加密

import hashlib
#叠加性
m=hashlib.md5()
m.update(b'say')
m.update(b'hello')
print(m.hexdigest())# 981fe96ed23ad8b9554cfeea38cd334a

m.update(b'sayhello')
print(m.hexdigest())# 981fe96ed23ad8b9554cfeea38cd334a

hmac模块

作用

对字符加密,并且加上密钥

import hmac
m=hmac.new(b'maerzi')
m.update(b'hash123456')
print(m.hexdigest())# f82317e44545b0ab087109454814b5c4

m=hmac.new(b'jfhsjfhshsdnjsd')
m.uodate(b'hash123456')
print(m.hexdigest()) # 2a70fd0f13cb49357f40d326a4e071a2

typing模块

作用

与函数连用,控制函数参数的数据类型,提供了基础数据类型之外的数据类型

from typing import Iterable
def func(x:int,lt:Iterable)->list:
    return [1,2,3]
func(10,'123123')

requests模块

作用

爬虫,模拟浏览器对url发送请求,拿到数据

import requests
response=requests.get('https://ishuo.cn')
data=response.text
print(data)

re模块

作用

去字符串找符合某种特点的字符串

元字符

import re
# ^:以...开头
s='abcdabc'
res=re.findall('^ab',s)
print(res)#['ab']

res=re.findall('^bc',s)
print(res)#[]

# $:以...结尾
s='abcdabc'
res=re.findall('bc$',s)
print(res)

# .: 任意字符
s='abc红abc'
res=re.findall('abc.',s)
print(res)#['abc红']

# \d: 数字
s='sdsd12345sfsd'
res=re.findall('\d',s)
print(res)#['1', '2', '3', '4', '5']

# \w: 非空,数字字母下划线
s='gfg_45 4556csd'
res=re.findall('\w',s)
print(res)#['g', 'f', 'g', '_', '4', '5', '4', '5', '5', '6', 'c', 's', 'd']
res=re.findall('\w+',s)
print(res)#['gfg_45', '4556csd']

# \s:空 空格
s='gfg_45 4556csd'
res=re.findall('\s',s)
print(res)#[' ']

# \D: 非数字
s='sdfg4654562hjhg'
res=re.findall('\D',s)
print(res)#['s', 'd', 'f', 'g', 'h', 'j', 'h', 'g']

# \W: 空
s='sdfsd_78 45df'
res=re.findall('\W',s)
print(res)#[' ']

# \S: 非空
s='sdfsd_78 45df'
res=re.findall('\S',s)
print(res)#['s', 'd', 'f', 's', 'd', '_', '7', '8', '4', '5', 'd', 'f']

# +:‘+’前面的一个字符至少一个
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 = 'abcddddd abcd abc'
print(re.findall('abcd*',s))#['abcddddd', '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 cbc dbc'
print(re.findall('abc|bbc',s))#['abc', 'bbc']

# {}:{2}'{}'前面的字符2个 {3}'{}'前面的字符3个
s = 'abccabc abccc'
print(re.findall('abc{3}', s))#['abccc']

# {}:{1,2}'{}'前面的字符1-2个 {1,3}'{}'前面的字符1-3个
s = 'abccabc abccc'
print(re.findall('abc{1,3}', s))#['abcc', 'abc', 'abccc']

贪婪模式

# .(任意字符)*(0-无穷个)
s='abcdefgbbbbbbbbfdfjdsdbfdg'
print(re.findall('a.*g',s))#['abcdefgbbbbbbbbfdfjdsdbfdg']

非贪婪模式

# .(任意字符)*(0-无穷个)?(让他进入非贪婪模式)
s='abcdefgbbbbbbbbfdfjdsdbfdg'
print(re.findall('a.*?g',s))#['abcdefg']

bug

# .(任意字符)*(0-无穷个)?(让他进入非贪婪模式)
s='abcdefg'
print(re.findall('.*?',s))#['', '', '', '', '', '', '', '']

#匹配邮箱
s = '#@#@#@nickchen121@163.com$$$$////nick@qq.com$$#$#$[]]2287273393@162.com@$2423423lksdlfj#'
# \w(字母/数字/下划线)+(0-无穷个)@ \w(字母/数字/下划线)+(0-无穷个).com
print(re.findall('\w+@\w+.com', s))

了解:特殊构造

# a(?=\d)  :a后面是数字 但不要数字,不消耗字符串内容
s = 'a123 aaaa a234 abc'
print(re.findall('a(?=\d)',s))#['a', 'a']
print(re.findall('a(?=\w)',s))#['a', 'a', 'a', 'a', 'a', 'a']

函数

# compile
s = 'abcd abcddd abc'
res=re.compile('abcd*')
print(re.findall(res,s))#['abcd', 'abcddd', 'abc']

#  match: 从开头找一个,找到了就不找了,找不到就报错
s = 'ab abcddd abc'
res=re.match('abcd*',s)
print(res.group())#AttributeError: 'NoneType' object has no attribute 'group'

#  search: 从字符串找一个,找到了就不找了
s = 'ab abcddd abc'
res=re.search('abcd*',s)
print(res.group())#abcddd

#  split 分割
s = 'ab23423abcddd234234abcasdfjlasjdk234l23lk4j2kl34kl25k3j2kl3j5lkj'
print(re.split('\d+',s))#['ab', 'abcddd', 'abcasdfjlasjdk', 'l', 'lk', 'j', 'kl', 'kl', 'k', 'j', 'kl', 'j', 'lkj']

#  sub :替换
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\nabc', '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<name1>.)c(?P<name2>d)',s).groupdict())#{'name1': 'b', 'name2': 'd'}

#  超高级用法
s = 'abc123abc123'
print(re.sub('c(\d+)a',' ',s))#ab bc123
print(re.sub('c(?P<name>\d+)a',' \g<name> ',s))#ab 123 bc123

posted @ 2020-03-26 22:45  风啊风啊  阅读(141)  评论(0编辑  收藏  举报