python之time模块和hashlib模块
生活随笔
收集整理的這篇文章主要介紹了
python之time模块和hashlib模块
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
一、time模塊
import time print(time.strftime('%Y-%m-%d %H:%M:%S'))#獲取當前的格式化時間,time.strftime(format) print(time.time())#獲取當前的時間戳 #第一種:將格式化的時間轉(zhuǎn)換成時間戳 #思路:先將格式化時間轉(zhuǎn)成時間元組,然后再轉(zhuǎn)成時間戳 # time_tuple=time.strptime('2019-02-18 15:15:32','%Y-%m-%d %H:%M:%S')#將格式化時間轉(zhuǎn)換成時間元組,time.strptime(string[,format] # print(time.mktime(time_tuple)) #編寫一個將格式化時間轉(zhuǎn)換成時間戳的函數(shù) def str_to_timestamp(time_str=None,format='%Y-%m-%d %H:%M:%S'):if time_str:time_tuple=time.strptime(time_str,format)timestamp=time.mktime(time_tuple)else:timestamp=time.time()return int(timestamp)#第二種,將時間戳轉(zhuǎn)換成格式化時間 #思路,首先將時間戳轉(zhuǎn)換成時間元組,然后再轉(zhuǎn)成格式化時間 # time_struct=time.gmtime(time.time())#將時間戳轉(zhuǎn)換成時間元組,標準時區(qū) # res=time.localtime(time.time())#將時間戳轉(zhuǎn)換成時間元組,當?shù)貢r區(qū) # res2=time.strftime('%Y-%m-%d %H:%M:%S',time_struct) # print(res2) #編寫一個將時間戳轉(zhuǎn)成格式化時間的代碼 def timestamp_to_str(timestamp=None,format='%Y-%m-%d %H:%M:%S'):if timestamp:time_struct=time.gmtime(timestamp)res=time.strftime(format,time_struct)else:res=time.strftime(format)return res二、hashlib模塊?
該模塊用于加密操作
import hashlib password='lyh123456' e=password.encode()#md5加密,必須先轉(zhuǎn)換成二進制才能加密 m=hashlib.md5(e) m1=hashlib.sha1(password.encode()) print(m) print(m.hexdigest())#加密后的16進制結(jié)果 print(m1.hexdigest())?
轉(zhuǎn)載于:https://www.cnblogs.com/balllyh/p/10396249.html
總結(jié)
以上是生活随笔為你收集整理的python之time模块和hashlib模块的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java-IO-基本输出输入流
- 下一篇: [Apple开发者帐户帮助]二、管理你的