目录
- 模块介绍
- time和datetime模块
- random
- os
- sys
- shutil
- json和pickle
- shelve
- xml处理
- yaml处理
- configparser
- hashlib
- re正则表达式
1. 模块介绍
1.1 定义
能够实现某个功能的代码集合(本质是py文件) test.p的模块名是test包的定义:用来从逻辑上组织模块,本质就是一个目录(必须带有一个__init__.py文件)
1.2 导入方法
a) Import module
b) Import module1,module2
c) From module import *
d) From module import m1,m2,m3
e) From module import logger as module_logger
1.3 Import 本质
导入模块的本质就是把python文件解释一遍
导入包的本质就是在执行该包下的__init__.py文件
1.4 导入优化
From module import test as module_test
1.5 模块的分类
a) 标准库
b) 开源模块(第三方模块)
c) 自定义模块
2. time & datetime 模块
time的三种表现方式:
1)时间戳(用秒来表示)
2)格式化的时间字符串
3)元组(struct_time)共九个元素。
2.1 时间戳
1 1 import time 2 3 2 # print(time.clock()) #返回处理器时间,3.3开始已废弃 , 改成了time.process_time()测量处理器运算时间,不包括sleep时间,不稳定,mac上测不出来 4 5 3 # print(time.altzone) #返回与utc时间的时间差,以秒计算\ 6 7 4 # print(time.asctime()) #返回时间格式"Fri Aug 19 11:14:16 2016", 8 9 5 # print(time.localtime()) #返回本地时间 的struct time对象格式 10 11 6 # print(time.gmtime(time.time()-800000)) #返回utc时间的struc时间对象格式 12 13 7 14 15 8 # print(time.asctime(time.localtime())) #返回时间格式"Fri Aug 19 11:14:16 2016", 16 17 9 #print(time.ctime()) #返回Fri Aug 19 12:38:29 2016 格式, 同上 18 19 10 20 21 11 # 日期字符串 转成 时间戳 22 23 12 # string_2_struct = time.strptime("2016/05/22","%Y/%m/%d") #将 日期字符串 转成 struct时间对象格式 24 25 13 # print(string_2_struct) 26 27 14 # struct_2_stamp = time.mktime(string_2_struct) #将struct时间对象转成时间戳 28 29 15 # print(struct_2_stamp) 30 31 16 #将时间戳转为字符串格式 32 33 17 # print(time.gmtime(time.time()-86640)) #将utc时间戳转换成struct_time格式 34 35 18 # print(time.strftime("%Y-%m-%d %H:%M:%S",time.gmtime()) ) #将utc struct_time格式转成指定的字符串格式 36 37 19 #时间加减 38 39 20 import datetime 40 41 21 # print(datetime.datetime.now()) #返回 2016-08-19 12:47:03.941925 42 43 22 #print(datetime.date.fromtimestamp(time.time()) ) # 时间戳直接转成日期格式 2016-08-19 44 45 23 # print(datetime.datetime.now() ) 46 47 24 # print(datetime.datetime.now() + datetime.timedelta(3)) #当前时间+3天 48 49 25 # print(datetime.datetime.now() + datetime.timedelta(-3)) #当前时间-3天 50 51 26 # print(datetime.datetime.now() + datetime.timedelta(hours=3)) #当前时间+3小时 52 53 27 # print(datetime.datetime.now() + datetime.timedelta(minutes=30)) #当前时间+30分 54 55 28 # c_time = datetime.datetime.now() 56 57 29 # print(c_time.replace(minute=3,hour=2)) #时间替换