WELCOME

不积跬步,无以至千里;不积小流,无以成江海。

Python常用模块--time、datetime、random、hashlib

1.time模块

除了使用datetime模块里的time类以外,Python还单独提供了另一个time模块,用来操作时间。time模块不仅可以用来显示时间,还可以控制程序,让程序暂停(使用sleep函数)

print(time.time())  # 获取从1970-01-01 00:00:00 UTC 到现在时间的秒数
print(time.strftime(
"%Y-%m-%d %H:%M:%S")) # 按照指定格式输出时间
print(time.ctime()) # 将时间戳转成字符串 print(time.sleep(
10)) # 让线程暂停10秒钟


print(time.strptime('2022/04/03', '%Y/%m/%d'))  # 将字符串转成元组的方式

print(time.mktime()) # 将元组转成时间戳的方式

 

 1 import time
 2 
 3 t = time.time()
 4 print(t)  # >> 1648968752.3602304
 5 
 6 time.sleep(3)
 7 t1 = time.time()
 8 print(t1)  # >> 1648968755.3625107
 9 
10 # 将时间戳转成字符串
11 s = time.ctime(t)
12 print(s)  # >> Sun Apr  3 14:53:44 2022(星期天 四月3号 14:53:44 2022年)
13 
14 # 将时间戳转成元组,可以取元组的值访问
15 t2 = time.localtime(t)
16 print(t2)
17 # >> time.struct_time(tm_year=2022, tm_mon=4, tm_mday=3, tm_hour=14, tm_min=55, tm_sec=28, tm_wday=6, tm_yday=93, tm_isdst=0)
18 
19 
20 # 将元组转成时间戳的方式
21 tt = time.mktime(t2)
22 print(tt)  # >> 1648969194.0 (去掉了精度值)
23 
24 # 时间戳--》元组---》字符串
25 t3 = time.strftime('%Y-%m-%d %H:%M:%S')
26 print(t3)  # >> 2022-04-03 15:07:16
27 
28 # 将字符串转成元组的方式
29 t4 = time.strptime('2022/04/03', '%Y/%m/%d')
30 print(t4)
31 # >> time.struct_time(tm_year=2022, tm_mon=4, tm_mday=3, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=6, tm_yday=93, tm_isdst=-1)

 

2.datatime模块

 

import datetime
print(datetime.date(2020, 1, 1))  # 创建一个日期
print(datetime.time(18, 23, 45)) # 创建一个时间
print(datetime.datetime.now())  # 获取当前的日期时间
datetime.timedelta(hours=2) # 时间差值
datetime.timedelta(days=2,hours=2) 有 days, seconds, microseconds, milliseconds, minutes, hours, weeks
print(datetime.datetime.now() + datetime.timedelta(3))  # 计算三天以后的日期时间

 

import datetime
import time

d = datetime.date(2022, 4, 3)  # >> 2022-04-03
print(d)
print(d.year)  # >> 2022

print(time.time())
print(datetime.date.ctime(d))  # >> Sun Apr  3 00:00:00 2022

# 获取当前日期和时间
print(datetime.datetime.now())  # >> 2022-04-03 16:21:49.720104

# 获取当前日期
print(datetime.date.today())  # >> 2022-04-03

# 时间差值
timedel = datetime.timedelta(hours=2)

print(timedel) # >> 2:00:00

now = datetime.datetime.now()
print(now) # >> 2022-04-03 16:30:40.741741
result = now + timedel
print(result) # >> 2022-04-03 18:30:40.741741

 

 

3.random模块

print(random.random())  # 生成 [0,1)的随机浮点数
print(random.uniform(20, 30))  # 生成[20,30]的随机浮点数
print(random.randint(10, 30))  # 生成[10,30]的随机整数
print(random.randrange(20, 30))  # 生成[20,30)的随机整数
print(random.choice('abcdefg'))  # 从非空序列中随机选择一个元素,像列表,元组,字典。。。
print(random.sample('abcdefghij', 3)) # 从列表里随机取出指定个数的元素
random.shuffle('黑桃A', '红桃J', '方块K') # 打乱非空序列的顺序

 

import random

ran = random.random()  # 0~1之间的随即小数
print(ran)

ran = random.randrange(1, 10, 2)  # random.randrange(1,10,2) 1~10 step=2 -->1,3,5,7,9
print(ran)  # 不包含10

ran = random.randrange(1, 10)
print(ran)

# 产生1~10之间的随即整数,包含10
ran = random.randint(1, 10)
print(ran)

# 从非空序列中随即取一个元素
list1 = ('bob', 'smith', 'jack', 'tom')
ran = random.choice(list1)
print(ran)

# 打乱顺序
pai = ['黑桃A', '红桃J', '方块K']
random.shuffle(pai)
print(pai)


# 验证码 大写字母与数字组合
# chr的作用就是将数字转换成对应的字母 对照ASCII码
def func():
    code = ''
    for i in range(4):
        ran1 = str(random.randint(0, 9))
        ran2 = chr(random.randint(65, 90))  # 随即产生26个大写字母
        ran3 = chr(random.randint(97, 122))  # 随即产生26个小写字母

        r = random.choice([ran1, ran2, ran3])

        code += r
    return code


code = func()
print(code)

 

# Unicode码 --> str
print(chr(65))  # >> A

# str --> Unicode码
print(ord('A'))  # >> 65

print(ord(''))  # >> 19979

print(chr(19978))  # >> 上

 

4.hashlib模块

  hashlib是一个提供字符加密功能的模块,包含MD5和SHA的加密算法,具体支持md5,sha1, sha224, sha256, sha384, sha512等算法。
该模块在用户登录认证方面应用广泛,对文本加密也很常见。 加密算法:md5, sha1, sha224, sha256, sha384, sha512,blake2b, blake2s, sha3_224, sha3_256, sha3_384, sha3_512, shake_128, shake_256(都不可逆)

 

import hashlib

msg = '不积跬步,无以至千里;不积小流,无以成江海'

md5 = hashlib.md5(msg.encode('utf-8'))
print('md5加密后:', md5.hexdigest())  # 十六进制加密后的
print(len(md5.hexdigest()))  # 32位
# >> md5加密后: 8b25f3b56b4231aeb01b4df2c6d1806f

sha1 = hashlib.sha1(msg.encode('utf-8'))
print('sha1加密后:', sha1.hexdigest())
print(len(sha1.hexdigest()))  # 40位
# sha1加密后: c0185d7fc08480d4ba27e89dc2fa744ffb420b74

sha256 = hashlib.sha256(msg.encode('utf-8'))
print('sha256加密后:', sha256.hexdigest())
print(len(sha256.hexdigest()))  # 64位
# >> sha256加密后: 47a5994525a8a39efcf1f302669c8d76ddf0fe2a9ae91b74f60e17bc584860ca

 

posted @ 2022-04-03 22:29  Ambitious~  阅读(72)  评论(0)    收藏  举报