目录
1、模块:
定义:其实模块简单说就是一堆代码实现某个功能,它们是已经写好的.py文件。只需要用import应用即可。
分类:
1、自定义模块
2、内置标准模块(又称标准库)
3、开源模块
1、自定义模块,就是自己写的.py文件为了实现某个功能。
2、内置模块,就是python自身已经写好的某个功能,例如经常用的sys、os等模块都是内置模块。
3、开源模块,开源大家都明白吧,就是不收费的别人写好的模块,一般也称第三方模块。
模块的引用:
1、import modules
2、from modules import 函数
3、如果有多个模块,可以用逗号隔开,如果想把某个模块里面的所有功能都导入,可以用*,这样的话功能都加载道内存里面,占用内存资源,不建议用。
2、time &datetime模块
!/usr/bin/env python
—*—coding:utf-8—*—
time和datetime
import time,datetime
##time
print(time.process_time())#返回处理器时间,3.3开始已废弃 , 改成了time.process_time()测量处理器运算时间,不包括sleep时间,不稳定,mac上测不出来
>>>0.09375
print(time.altzone)#返回与utc时间的时间差,以秒计算\
>>>-32400
print(time.asctime()) #返回时间格式"Mon Aug 22 09:31:43 2018",
>>>Mon Aug 22 09:31:43 2018
print(time.localtime())
>>>time.struct_time(tm_year=2016, tm_mon=8, tm_mday=22, tm_hour=9, tm_min=32, tm_sec=28, tm_wday=0, tm_yday=235, tm_isdst=0)
tm_year(年),tm_mon(月),tm_mday(日),tm_hour(时),tm_min(分),tm_sec(秒),tm_wday(星期,从0到6,0代表周一,一次类推),tm_yday(这一年中的地几天),tm_isdst(夏令时间标志)
print(time.time())#(获取时间戳)
a = time.time()
b = a/3600/24/365
print(b)
>>>46.67149458666888
2018-1970
>>>46
时间是村1970年开始算,为什么时间要从1970年开始算呢
1970年1月1日 算 UNIX 和 C语言 生日. 由于主流计算机和操作系统都用它,其他仪器,手机等也就用它了.
print(time.gmtime(time.time()-800000))#返回utc时间的struc时间对象格式
>>>time.struct_time(tm_year=2016, tm_mon=8, tm_mday=12, tm_hour=20, tm_min=9, tm_sec=14, tm_wday=4, tm_yday=225, tm_isdst=0)
print(time.asctime(time.localtime()))
>>>Mon Aug 22 10:23:52 2018
print(time.ctime())
>>>Mon Aug 22 10:24:25 2018
string_2_struct = time.strptime("2016/05/22","%Y/%m/%d")#将时间字符串转换成struct时间对象格式
print(string_2_struct)
>>>time.struct_time(tm_year=2016, tm_mon=5, tm_mday=22, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=6, tm_yday=143, tm_isdst=-1)
string_3_struct = time.strptime("2016/05/22","%Y/%m/%d")
struct_2_stamp = time.mktime(string_3_struct) #将struct时间对象转成时间戳
print(struct_2_stamp)
>>>1463846400.0
print(time.gmtime(time.time()-86640))##将utc时间戳转换成struct_time格式
>>>time.struct_time(tm_year=2016, tm_mon=8, tm_mday=21, tm_hour=2, tm_min=25, tm_sec=14, tm_wday=6, tm_yday=234, tm_isdst=0)
a = time.timezone
print(a/3600)
>>>-8.0东八区
time.sleep()#睡几秒
time.localtime()#当前本地时间
strftime()#重要!
a = time.localtime()#将本地时间格式化
time = time.strftime("%Y/%m/%d %H:%M:%S",a)
print(time)
>>>2018/08/22 10:31:07
前
前后刚好相反
后
# time.strptime()#前后一定要位置一一对应(注意符号空格)
time1 = time.strptime("2016-08:20 14:31:52","%Y-%m:%d %H:%M:%S")
print(time1)
####datetime(时间的加减)
print(datetime.datetime.now()) #返回 2018-08-22 10:33:53.290793
print(datetime.date.fromtimestamp(time.time()) ) #2018-08-22
print(datetime.datetime.now() )#2016-08-22 10:34:37.885578当前时间一般使用这个
print(datetime.datetime.now() + datetime.timedelta(3)) #当前时间+3天#2018-08-25 10:35:38.554216
print(datetime.datetime.now() + datetime.timedelta(-3)) #当前时间-3天#2018-08-19 10:35:58.064103
print(datetime.datetime.now() + datetime.timedelta(hours=3)) #当前时间+3小时#2018-08-22 13:36:16.540940
print(datetime.datetime.now() + datetime.timedelta(minutes=30)) #当前时间+30分#2018-08-22 11:06:34.837476
c_time = datetime.datetime.now()
print(c_time.replace(minute=3,hour=2)) #时间替换
3、random
#!/usr/bin/env python
#—*—coding:utf-8—*—
import random
随机数
print(random.random())#0到的随机数,是一个float浮点数
print(random.randint(1,2))#一和二随机
print(random.randrange(1,10))#一到十随机(记得range只有头没有尾巴)
生成随机验证码
checkcode=''
for i in range(5):
current = random.randrange(0,5)
if current == i:
tmp = chr(random.randint(65,90))#chr是ascii码表,65,90是(A-Z)
else:
tmp = random.randint(0,9)
checkcode +=str(tmp)
print(checkcode)
#二般
import string
print(''.join(random.sample(string.ascii_lowercase+string.digits,5)))#随机验证码可用(5位含数字和密码)
print(''.join(random.sample(string.ascii_lowercase+string.digits,5)))#随机验证码可用(5位含数字和密码)
#打印a-z
print(string.ascii_lowercase)#一
#打印A-Z
print(string.ascii_letters)#二
4、os
!/usr/bin/env python
import os
# print(os.getcwd())#打印当前目录
os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径
os.chdir("dirname") 改变当前脚本工作目录;相当于s