day13 —— 学习总结
常用的模块
-
工作中
-
os模块 — 文件操作系统(主要提供文件和文件夹相关操作)
listdir(目录地址) — 返回指定目录下所有文件的文件名
result = os.listdir('./files') print(result)
-
sys模块 — 系统相关操作 ,例如: exit() — 结束线程
-
json 模块 — json数据 数据处理
-
re模块 — 正则表达式相关操作
-
math模块
ceil(浮点数) — 将浮点数转换成整数(向大取整)
floor(浮点数) — 将浮点数转换成整数(向小取整)
print(int(12.9999)) # 12, 直接去掉小数点 print(int(-12.78)) # -12 print(math.ceil(12.9)) # 13 , 向大取整 print(math.ceil(-12.9)) # -12 print(math.floor(12.99)) # 12 , 向小取整 print(math.floor(-12.9)) # -13 print(round(12.6)) # 13,四舍五入取整 print(round(12.1)) # 12
-
*cmath 模块 — 复数的数学模块
complex — 所有复数对应的类型
-
时间相关模块: time 、datetime
-
hashlib — 生成哈希摘要
-
csv — csv文件操作(表格文件操作)
-
-
生活中
- turtle — 画图
- pygame(第三方) — 游戏开发
- itchat(第三方) — 微信机器人
- smtplib 和 email — 邮件自动发送和接收
- reportlab(第三方) — pdf文件操作 (canvas — 创建PDF水印)
- scoket — 网络通信
hashlib的使用
- 什么是hash加密
- hash加密的密文(摘要)是不可逆的
- 相同的数据通过相同的算法生成的密文(摘要)是一样的
- 不同大小的数据通过相同的算法生成的密文(摘要)的长度是一样的
- hash相关算法:md5 和 shaXXX
- 生成摘要
- 创建hash对象 — hashlib.算法名()
- 添加原数据 — hash对象.update(二进制数据)
- 获取摘要 — hash对象.hexdigest()
时间模块(import time)
-
time模块
1.1时间戳(时间差)
-
用某一个时间到1970年1月1日0时0分0秒(格林威治时间)的时间差来表示一个时间就是时间戳(单位:秒)
-
同一个时间:时间戳保存4个字节
-
time() — 获取当前时间 ,返回时间戳
t1 = time.time() print(t1) # 1612420479.410478 '2021-2-4 14:23:09'
-
本地时间
-
localtime() — 获取当前本地时间,返回结构体时间(结构体时间中tm_wday表示星期,用0~6表示星期一到星期天
-
localtime(时间戳) — 将时间戳对应的时间转换成本地的结构体时间
t2 = time.localtime() print(t2, t2.tm_year, t2.tm_wday) t3 = time.localtime(1612420479.410478) print(t3)
-
-
-
datetime模块(import datetime)
2.1 获取当前日期
t4 = datetime.date.today() print(t4, t4.year, t4.month)
2.2 获取当前时间
t5 = datetime.datetime.now() print(t5, t5.year, t5.day, t5.hour)
2.3 时间的加减操作
# 让时间t5减10天 value = datetime.timedelta(days=10) print(t5 - value) # 让时间t5减24小时 value = datetime.timedelta(hours=24) print(t5 - value) # 让时间天加30天零2个小时 value = datetime.timedelta(days=30, hours=2) print(t5 + value)
二进制和字符串之间的转换
-
字符串(str) 转二进制(bytes)
- 方法一:bytes(字符串, encoding = ‘utf-8’)
- 方法二:字符串.encode()
result = bytes('护士', encoding='utf-8') print(result) result = '护士'.encode() print(result, type(result))
-
二进制转字符串
- 方法一:str(二进制, encoding= ‘utf-8’)
- 方法二:二进制.decode()
s = str(result, encoding='utf-8') print(s, type(s)) s = result.decode() print(s)
文件操作
-
数据持久化(数据本地化)
- 默认情况下程序中所有的数据都是保存在运行内存中的,运行内存中的数据在程序运行结束后会自动销毁。
- 只要将数据存储在硬盘中,数据才会一直存在。
- 如果想要将程序中产生的数据保存到硬盘中,需要先将数据存储到文件中。(常见的文件:数据库文件、plist文件、json文件、txt文件…)
-
文件操作 — 操作文件中的内容
-
基本步骤 : 打开文件 — 操作文件(读、写) — 关闭文件
-
2.1 打开文件:
-
open(file, mode=‘r’,*,encoding=None) - 打开文件并且返回一个文件对象
-
file — 需要打开的文件的路径(地址)
- a. 绝对路径: 文件或者文件夹在计算机中的全部路径
- b. 相对路径: 写路径的时候只写全路径中的一部分,剩下的部分用特殊符号代替
- . — 表示当前目录(当前代码文件所在的目录),可以省略
- … — 表示当前目录的上层目录
- … — 表示当前目录的上层目录的上层目录
-
mode — 文件打开方式(决定打开文件后能做什么、操作文件的时候对应的数据类型
- 第一组值:决定打开后能做什么 读 / 写
- r — 只读
- w — 只写, 打开后会清空原文件中的内容
- a — 只写, 打开后不会清空原文件中的内容
- 第二组值: 决定操作文件数据的时候对应的数据类型,字符串 / 二进制 默认是 t
- t — 文本数据,对应的类型是str(默认)
- b — 二进制数据, 对应的类型是bytes
- 第一组值:决定打开后能做什么 读 / 写
-
两组数据中必须每一组选择一个值: ‘r’ == ‘rt’ 、 ’tr‘
格式: ’wb‘ 、 ’bw‘
-
encoding — 设置文本文件的编码方式 , 一般设置成utf-8
如果是以t的形式打开一个文本文件的时候需要设置encoding
打开方式带b绝对不能设置encoding
r - 只读 f = open(r'./files/test.txt', 'r') f.read() f.write('abc') # io.UnsupportedOperation: not writable a - 只写 f = open(r'./files/test.txt', 'a') f.write('abc') f.read() # io.UnsupportedOperation: not readable w - 只写,清空 f = open(r'./files/test.txt', 'w') f.write('xyz') f.read() # io.UnsupportedOperation: not readable t/b - 操作数据是字符串 f = open(r'./files/test.txt', 'rb') result = f.read() print(result, type(result)) f = open(r'./files/test.txt', 'ab') f.write('abc') # TypeError: a bytes-like object is required, not 'str' f.write('123abc123'.encode()) 注意:打开二进制文件必须带 b f = open(r'files/luffy4.jpg', 'rb') f.read() r和a、w打开不存在的文件 - r会报错;w和a不报会新建 open(r'./files/bbb.txt', 'r') # FileNotFoundError open(r'./files/ccc.txt', 'a')
-
-
2.2 读写文件
-
1 读
-
a. 文件对象.read() — 从读写位置开始读到文件结束(结束的时候读写位置在文件末尾)
- 文件名 . seek(0) — 将读写位置设置到文件开头
f = open('files/test.py', 'rt', encoding='utf-8') result = f.read() print(result) print('=======================') f.seek(0) # 将读写位置设置到文件开头,不然读取不到 result = f.read() print(result)
-
b.文件对象.readline() — 从读写位置开始到一行的末尾,光标回到下一行的开始, (只针对以t打开的文本文件)
f = open('files/test.py', 'rt', encoding='utf-8') result = f.readline() print(result) result = f.readline() print(result) print('========================') result = f.read() print(result)
-
-
2 写
- 文件对象 . write(数据)
f = open(f'files/test.py', 'a', encoding='utf-8') f.write('\nprint("hello world")') # 写入内容会出现在文件的末尾处,为防止出错 添加\n换行
-
3 关闭文件
- 文件对象 . close()
f.close()
-
-