Day15-模块
math、cmath
math(普通数学模块)、cmath(复数的数学模块)
random
import random
"""
random.randint(m,n) - 产生[m, n]的随机数
random.random() - 产生[0,1)的随机数
random.shuffle(列表) - 随机打乱列表中的元素
random.choice(列表) - 随机取出一个列表元素
random.choices(列表, k=n) - 随机取出多个元素,可能会重复
"""
os
"""
os.chdir(路径) - 将指定路径对应的目录修改成当前目录
(默认情况下当前目录是当前py文件所在的目录)
os.getced() - 获取当前的目录的全路径
创建文件/文件夹、删除文件/文件夹、判断是否是文件/文件夹、判断文件/文件夹是否存在
os.open(path,os.O_CREAT) - 创建并打开一个新文件
os.mkdir(path,mode) - 创建一个文件夹
os.makedirs(name) - 当前py文件目录创建一个新文件
os.removedirs(name) - 删除当前py文件目录下的文件
os.remove(path) - 删除文件,如果是文件夹抛出OSError
os.rmdir(path) - 删除空目录(空文件夹),如果目录非空,抛出OSError
os.path.isfile(path) - 判断是否是文件
os.path.isdir(path) - 判断是否是文件夹
os.path.exists(path) - 判断文件或者文件夹是否存在
json
re - 正则表达式相关的操作
sys
sys.exit() 提前结束线程
turtle
csv
smtplib
time 和 dateTime
import time
print(time.asctime())
# 什么是时间戳
"""
时间戳就是以秒为单位表示的到格林威治时间1970年1月1日0时0分0秒时间差
保存时间戳要比直接保存时间信息更加节省内存
对时间戳进行加密比对字符串时间加密要方便
"""
# 1.time() - 获取当前时间(返回的是当前时间的时间戳)
t1 = time.time()
print(t1) # 1603093170.7906544
# 2.time.localtime()
# time.localtime() - 获取当前的本地时间
t2 = time.localtime()
print(t2)
# time.localtime(时间戳) - 获取指定时间戳对应的本地时间,返回struct_time
t3 = time.localtime(0)
print(t3)
t4 = time.localtime(1603093170.7906544)
print(t4, type(t4))
# 3.time.strftime(时间格式字符串,结构体时间) - 将结构体时间转换成指定格式的字符串时间
s1 = time.strftime('%Y/%m/%d', t4)
print(s1)
s2 = time.strftime('%Y/%m/%d %H:%M:%S', t4)
print(s2)
# 星期一 下午3:38
s3 = time.strftime('%B %A %p%H:%M', t4)
print(s3)
def china_time(t_str: str):
table = {
'PM': '下午',
'AM': '上午'
}
for key in table:
t_str = t_str.replace(key, table[key])
return t_str
print(china_time(s3))
# time.strptime(字符串, 时间格式字符串) - 将字符串时间转换成结构体时间
s2 = '1999-02-17'
t5 = time.strptime(s2, '%Y-%m-%d')
print(t5)
hashlib
import hashlib
"""
hashlib模块主要提供hash加密相关算法来对数据进行加密处理
hash加密(hash摘要)的特点:
a.加密结果不可逆(不能通过密文/摘要去获取到原文)
b.同一个数据通过相同的算法加密之后的结果是一样的
c.不同的数据通过相同的算法加密之后的长度相同
hash摘要的应用场景:
a.密码保存
b.数据完整性的验证
"""
# 1.根据算法创建hash对象
# hashlib.md5()
hax = hashlib.md5()
# 2.添加需要生成摘要/密文的数据
# hash对象.update(数据的二进制)
hax.update('123123'.encode(encoding='utf-8'))
# 3.生成摘要
dig = hax.hexdigest()
print(dig)