day13 包和模块
一、模块的使用
1,什么是模块
python中一个py就是一个模块
2.怎么在一个模块中使用另外一个模块中的内容(内容包括:数据、函数、类等)
注意:1)如果要在一个模块中使用另外一个模块中的内容,被使用的模块名必须是标识符并且不是关键字
2)一个模块中只有全局变量才能被其他模块使用
3)其他模块只有在被导入以后才能被使用
3.怎么导入模块
-
import 模块名 - 导入指定模块,导入后可以通过‘模块名.变量’的方式使用被导入的模块中所有的全局变量
2)from 模块名 import 变量1,变量2,… - 导入指定模块中指定变量,导入后直接使用指定变量
3)from 模块名 import * - 导入指定模块中所有的变量,导入直接使用 -
重命名
import 模块名 as 新模块名 - 给模块重命名。重命名后使用新的名字来代替原模块名
from 模块名 import 变量1 as 新变量1,变量2,变量3,… - 给指定变量重命名
#方式一:import 模块名
import test
print(test.a)
print(test.name)
test.test_func1()
# 方式二:from 模块名 import 变量1,变量2,...
from test import name, test_func1
print(name)
test_func1()
# print(a) # 报错!
方式三:from 模块名 import *
from test import *
print(a)
print(name)
test_func1()
方式四:给模块重命名
import test as tt
test = 1000
print(f'test:{test}')
tt.test_func1()
方法五:给变量重命名
from test import name as t_name, a
name = '小明'
print(name, t_name ,a)
4.导入模块的原理
执行代码的时候如果遇到import或者from-import导入模块的时候,系统会自动进入指定模块,将模块中的代码全部执行一遍
5.重复导入
import导入模块的时候会自动检测被导入的模块是否已经导入过,如果导入过不会重复导入。
(在任何位置需要任何模块的时候直接导,不需要有任何重复导入的负担)
二、包的使用
1.什么是包
包就是包含_init_.py文件的文件夹。包就是专门用来对模块进行封装的。
2.包的使用(导入包中的模块、或者导入包中模块的内容
1)import 包.模块
2) import 包.模块 as 新名字
3)from 包 import 模块1,模块2,…
4)from 包.模块 import 变量1,变量2…
# 导入方式一:
import fileManager.excelFile
fileManager.excelFile.read_line()
# 导入方式二:
import fileManager.excelFile as excelFile
excelFile.read_line()
# 导入方式三:
from fileManager import excelFile, jsonFile
excelFile.read_col()
jsonFile.write_json_file()
# 导入方式四:
from fileManager.textFile import read_file, open_file
read_file()
open_file()
3.导入包中的内容原理
通过包导入模块或者模块内容的时候,系统会先执行包中_init_.py文件,然后再执行对应的模块
# from fileManager.textFile import open_file
import fileManager
fileManager.open_file()
from fileManager import open_file
open_file()
三、random模块
import random
1.随机整数
random.randint(m, n) - 产生m到n的随机整数
print(random.randint(10, 20))
2.随机浮点数
random.random() - 产生0~1的随机小数:[0,1)
print(random.random()) # [0, 1)
print(random.random() * 100) # (0, 100)
print(random.random() * 80 + 20) # (20, 100)
3.随机打乱列表中元素的顺序
random.shuffle(列表)
nums = [1, 2, 3, 4, 5, 6, 7, 8]
random.shuffle(nums)
print(nums)
4.随机获取元素
random.choice(序列) - 随机获取序列中的一个元素
random.choices(序列,k=N) - 随机获取序列中N个元素
nums = [1, 2, 3, 4, 5, 6, 7, 8]
print(random.choice(nums))
print(random.choices(nums, k=2))
产生唯一文本信息
import uuid
print(uuid.uuid1())
# print(uuid.uuid3())
print(uuid.uuid4())
# print(uuid.uuid5())
四、时间相关模块
import time
时间戳
时间戳是通过保存某个时间点到1970年1月1日0时0分0秒(格林威治时间)的时间差来保存时间的,单位是秒
保存时间戳比保存字符串时间更节约内存
time模块
1.time.time() - 获取当前时间的时间戳
t1 = time.time()
print(t1) # 1620978956.6019998
2.localtime
time.localtime() - 获取当前当地时间,返回的是结构体时间
time.localtime(时间戳) - 将时间戳转换成结构体时间
t2 = time.localtime()
print(t2) # time.struct_time(tm_year=2021, tm_mon=5, tm_mday=14, tm_hour=16, tm_min=15, tm_sec=6, tm_wday=4, tm_yday=134, tm_isdst=0)
print(t2.tm_year, t2.tm_mon, t2.tm_mday)
t3 = time.localtime(0)
print(t3)
t4 = time.localtime(1620978956.6019998)
print(t4)
3.time.sleep(N) - 睡眠指定时间(单位是秒)
print('======')
time.sleep(1)
print('++++++')
datetime模块
from datetime import datetime, time, date, timedelta
1.获取当前时间
t1 = datetime.now()
print(t1, type(t1)) # 2021-05-14 16:45:14.308800 <class 'datetime.datetime'>
print(t1.year, t1.month, t1.day, t1.hour, t1.minute, t1.second)
t2 = date.today()
print(t2)
2.获取时间对应的时间戳
datetime对象.timestamp() - 获取时间对象对应的时间戳
t3 = t1.timestamp()
print(t3)
3.将字符串时间转换成时间对象
datetime.strptime(时间字符串,时间格式字符串)
%Y - 年
%m - 月
%d - 日
%H - 时(24小时制)
%M - 分
%S - 秒
t_str = '2015-3-4 18:30:25'
t4 = datetime.strptime(t_str, '%Y-%m-%d %H:%M:%S')
print(t4, type(t4)) # 2015-03-04 08:30:25 <class 'datetime.datetime'>
print(t4.timestamp())
t_str = '2010/8/4'
t5 = datetime.strptime(t_str, '%Y/%m/%d')
print(t5)
4.时间的加减操作
t_str = '2015-12-31 23:59:59'
t6 = datetime.strptime(t_str, '%Y-%m-%d %H:%M:%S')
print(t6 + timedelta(seconds=1)) # 2010-08-04 00:00:00
print(t6 + timedelta(days=10)) # 2016-01-01 00:00:00
print(t6 - timedelta(days=1, hours=1)) # 2015-12-30 22:59:59