时间模块:
python表示时间的三种方式:
在Python中,通常有这三种方式来表示时间:时间戳、元组(struct_time)、格式化的时间字符串:
时间戳(timestamp) :时间戳表示的是从伦敦时间1970年1月1日00:00:00开始按秒计算的偏移量。因为伦敦时间是0时区 换成北京时间的话 是1970年1月1日8:00:00 我们运行“type(time.time())”,返回的是float类型。
格式化的时间(Format String):是一个字符串类型 比如 ‘1999-12-06’
结构化时间(struct_time) : 是一个元组 共有9个元素共九个元素:(年,月,日,时,分,秒,一年中第几周,一年中第几天等)
小结:时间戳是计算机能够识别的时间;时间字符串是人能够看懂的时间;元组则是用来操作时间的
格式化时间的符号;
- %y 两位数的年份表示(00-99)
- %Y 四位数的年份表示(000-9999)
- %m 月份(01-12)
- %d 月内中的一天(0-31)
- %H 24小时制小时数(0-23)
- %I 12小时制小时数(01-12)
- %M 分钟数(00=59)
- %S 秒(00-59)
- %a 本地简化星期名称
- %A 本地完整星期名称
- %b 本地简化的月份名称
- %B 本地完整的月份名称
- %c 本地相应的日期表示和时间表示
- %j 年内的一天(001-366)
- %p 本地A.M.或P.M.的等价符
- %U 一年中的星期数(00-53)星期天为星期的开始
- %w 星期(0-6),星期天为星期的开始
- %W 一年中的星期数(00-53)星期一为星期的开始
- %x 本地相应的日期表示
- %X 本地相应的时间表示
- %Z 当前时区的名称
- %% %号本身
结构化时间(元组)内的元素是什么:
索引(Index) | 属性(Attribute) | 值(Values) |
---|---|---|
0 | tm_year(年) | 比如2011 |
1 | tm_mon(月) | 1 - 12 |
2 | tm_mday(日) | 1 - 31 |
3 | tm_hour(时) | 0 - 23 |
4 | tm_min(分) | 0 - 59 |
5 | tm_sec(秒) | 0 - 60 |
6 | tm_wday(weekday) | 0 - 6(0表示周一) |
7 | tm_yday(一年中的第几天) | 1 - 366 |
8 | tm_isdst(是否是夏令时) | 默认为0 |
时间格式之间的转换:
时间戳 --> 结构化时间:
time.gmtime(时间戳) #转成UTC时间,与英国伦敦当地时间一致
time.localtime(时间戳) #转成当地时间。例如我们现在在北京执行这个方法:与UTC时间相差8小时,UTC时间+8小时 = 北京时间
print(time.gmtime(1500000000)) time.struct_time(tm_year=2017, tm_mon=7, tm_mday=14, tm_hour=2, tm_min=40, tm_sec=0, tm_wday=4, tm_yday=195, tm_isdst=0) print(time.localtime(1500000000)) time.struct_time(tm_year=2017, tm_mon=7, tm_mday=14, tm_hour=10, tm_min=40, tm_sec=0, tm_wday=4, tm_yday=195, tm_isdst=0)
结构化时间 -->时间戳
time.mktime(结构化时间)
time_tuple = time.localtime(1500000000) print(time.mktime(time_tuple)) #1500000000.0
结构化时间 -->字符串时间:
time.strftime("格式定义","结构化时间") 结构化时间参数若不传,则显示当前时间
print(time.strftime("%Y-%m-%d %X")) #2018-09-04 16:06:31 print(time.strftime("%Y-%m-%d",time.localtime(1500000000))) #'2017-07-14'
字符串时间--> 结构化时间:
time.strptime(时间字符串,字符串对应格式)
print(time.strptime("2018-09-04","%Y-%m-%d")) #time.struct_time(tm_year=2018, tm_mon=9, tm_mday=4, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=1, tm_yday=247, tm_isdst=-1) print(time.strptime("07/24/2017","%m/%d/%Y")) #time.struct_time(tm_year=2017, tm_mon=7, tm_mday=24, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=0, tm_yday=205, tm_isdst=-1)
结构化时间 --> %a %b %d %H:%M:%S %Y串:
time.asctime(结构化时间) 如果不传参数,直接返回当前时间的格式化串
print(time.asctime(time.localtime(1500000000))) #Fri Jul 14 10:40:00 2017 print(time.asctime()) #Tue Sep 4 16:12:31 2018
时间戳 --> %a %b %d %H:%M:%S %Y串:
time.ctime(时间戳) 如果不传参数,直接返回当前时间的格式化串
print(time.ctime()) #Tue Sep 4 16:12:31 2018 print(time.ctime(1500000000)) #Fri Jul 14 10:40:00 2017
计算时间差:
import time true_time=time.mktime(time.strptime('1996-03-20 08:30:00','%Y-%m-%d %H:%M:%S')) time_now=time.mktime(time.strptime('2018-09-4 11:00:00','%Y-%m-%d %H:%M:%S')) dif_time=time_now-true_time struct_time=time.gmtime(dif_time) print('过去了%d年%d月%d天%d小时%d分钟%d秒'%(struct_time.tm_year-1970,struct_time.tm_mon-1, struct_time.tm_mday-1,struct_time.tm_hour, struct_time.tm_min,struct_time.tm_sec))
random模块(随机数模块):
取随机小数:
random.random()
random.uniform()
print(random.random()) #取(0,1)之间 不接受参数 print(random.uniform(2,3)) #(n,m)可接受最大值和最小值
取随机整数:
random.randint(1,2)
random.randrange()
print(random.randint(1,2)) # [1,2] 随机取一个固定的数 print(random.randrange(1,3)) # #取range范围数 顾头不顾腚 print(random.randrange(1,100,2)) #可加步长
从一个列表里随机抽取:
random.choise()
lst = [1,2,3,4,5,('a','b'),'cc','dd'] ret = random.choice(lst) print(ret) ret = random.choice(range(100)) print(ret) ret = random.sample(lst,3) #从列表里取三个值 print(ret)
乱序:
random.shuffle()
lst = [1,2,3,4,5,('a','b'),'cc','dd'] ret = random.choice(lst) print(ret) ret = random.choice(range(100)) print(ret) ret = random.sample(lst,3) #从列表里取三个值 print(ret)
验证码函数:
def get_code(n = 6,alph_flag = True): #默认是6位验证码 默认数字大小写英文都有 code = '' for i in range(n): c = str(random.randint(0,9)) if alph_flag: alpha_upper = chr(random.randint(65, 90)) alpha_lower = chr(random.randint(97, 122)) c = random.choice([c,alpha_upper,alpha_lower]) code += c return code print(get_code())