1,工作中的常用模块
① os模块 - 文件操作系统(主要提供文件和文件夹相关操作)
listdir(目录地址) - 返回指定目录下所有文件的文件名
import os
result = os.listdir('./files')
print(result)
② sys模块 - 系统相关操作, 例如:exit() - 结束线程
③ json模块 - json数据数据处理(后面讲)
④ re模块 - 正则表达式相关操作(后面讲)
⑤ math模块
ceil(浮点数) - 将浮点数转换成整数(向大取整)
floor(浮点数) - 将浮点数转换成整数(向小取整)
print(int(12.9999)) # 12, 直接去掉小数点
print(int(-12.78)) # -12
import math
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 - 所有复数对应的类型
a = 10+2j
print(type(a))
b = 30 - 3j
print(a + b) # (40-1j)
print(a * b) # (306+30j)
⑦ 时间相关模块:time、datetime
⑧ hashlib - 生成哈希摘要
⑨ csv - csv文件操作(表格文件操作)
2,生活中的常用模块和库
① turtle - 画图(哄小孩儿)
② pygame(第三方) - 游戏开发
③ itchat(第三方) - 微信机器人
④ smtplib和email - 邮件自动发送和接收
⑤ reportlab(第三方) - pdf文件操作 (canvas - 创建pdf水印)
⑥ socket - 网络通信
3,hash加密
1)hash加密的密文(摘要)是不可逆的
2)相同的数据通过相同的算法生成的密文(摘要)是一样的
3)不同大小的数据通过相同的算法生成的密文(摘要)的长度是一样
hash相关算法:md5和shaXXX
# 1) 创建hash对象
# hashlib.算法名()
hash = hashlib.md5()
# 2) 添加原数据
# hash对象.update(二进制数据)
hash.update('123456'.encode())
# 3) 获取摘要
# hash对象.
result = hash.hexdigest()
print(result) # 'e10adc3949ba59abbe56e057f20f883e'
4,time模块
① 时间戳(时间差)
用某一个时间减1970年1月1日0时0分0秒(格林威治时间)的时间差来表示一个时间就是时间戳(单位:秒)
同一个时间:时间戳保存4个字节, 用字符串需要17个字节
# 1)time() - 获取当前时间,返回时间戳
import 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
t4 = datetime.date.today()
print(t4, t4.year, t4.month) # 2021-02-04 2021 2
# 获取当前时间
t5 = datetime.datetime.now()
print(t5, t5.year, t5.day, t5.hour)
# 时间的加减操作
# 让时间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)
5,文件操作
① 数据持久化(数据本地化)
默认情况下程序中所有的数据都是保存在运行内存中的,运行内存中的数据在程序运行结束后会自动销毁。
只有将数据存储在硬盘中,数据才会一直存在。如果想要将程序中产生的数据保存到硬盘中,需要先将数据存储到文件中。(常见的文件:数据库文件、plist文件、json文件、txt文件…)
② 文件操作 - 操作文件中的内容
基本步骤:打开文件 -> 操作文件(读、写) -> 关闭文件
打开文件
open(file, mode='r’,*,encoding=None) - 打开文件并且返回一个文件对象
-
file - 需要打开的文件的路径(地址)
a.绝对路径:文件或者文件夹在计算机中的全路径
例如:/Users/yuting/授课/Python2101/01语言基础/day13-常用模块和文件操作/files/test.txt
b.相对路径:写路径的时候只写全路径的一部分,剩下的部分用特殊符号代替
. - 表示当前目录(当前代码文件所在的目录),可以省略
… - 表示当前目录的上层目录
… - 表示当前目录的上层目录的上层目录 -
mode - 文件打开方式(决定打开文件后能做什么、操作文件的时候对应的数据类型)
第一组值:决定打开后能做什么,读/写?
r - 只读
w - 只写,打开后会清空源文件中的内容
a - 只写,打开后不会清空源文件内容
第二组值:决定操作文件数据的时候对应的数据类型,字符串/二进制?默认是 t
t - 文本数据,对应的类型是str(默认)
b - 二进制数据,对应的类型是bytes
两组数据中必须每一组选择一个值:‘r’ == ‘rt’、‘tr’、格式:‘wb’、‘bw’
3)encoding - 设置文本文件的编码方式,一般设置成utf-8
如果是以t的形式打开一个文本文件的时候需要设置encoding。
打开方式带b绝对不能设置encoding
# 绝对路径
open(r'/Users/yuting/授课/Python2101/01语言基础/day13-常用模块和文件操作/files/test.txt')
# 相对路径
open(r'./files/test.txt') # 文件路径前加R或者r可以避免转义字符
open(r'./aaa.txt')
open(r'../day13-常用模块和文件操作/files/test.txt')
# 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() - 从读写位置开始读到文件结束(结束的时候读写位置在文件末尾)
# 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")')
# 2.3 关闭文件
# 文件对象.close()
f.close()
# f.write('ksskskksk') # ValueError: I/O operation on closed file.
6,二进制和字符串之间的转换
# 1.字符串(str)转二进制(bytes)
# 方法一: bytes(字符串, encoding='utf-8')
# 方法二:字符串.encode()
result = bytes('护士', encoding='utf-8')
print(result)
result = '护士'.encode()
print(result, type(result)) # b'\xe6\x8a\xa4\xe5\xa3\xab' <class 'bytes'>
# 2.二进制转字符串
# 方法一:str(二进制, encoding='utf-8')
# 方法二:二进制.decode()
s = str(result, encoding='utf-8')
print(s, type(s))
s = result.decode()
print(s) # 护士