論Python常見的內建模組

2021-09-27 06:55:37 字數 3233 閱讀 1981

sys

hashlib

hmac

base64

time

datetime

sys.ar**() # 在python指令碼傳參使用

sys.exit() # 系統退出

sys.getdefaultencoding() # 獲取系統預設編碼

getfilesystemencoding() # 獲取檔案編碼

getrecursionlimit() # 獲取系統預設遞迴的最大層數

setrecursionlimit(num) # 設定遞迴的最大層數

getrefcount() # 獲取物件的引用計數的數量

import sys

print(sys.ar**)

print(sys.version)

print(sys.maxint)

print(sys.path)

print(sys.platform)

print(sys.exit(0))

加密,雜湊加密(hash加密)

加密是否可逆:

|-- 可逆加密

根據加密和解密的秘鑰是否是同乙個

|-- 對稱加密

des|-- 非對稱加密

rsa|-- 不可逆加密

hash是典型的不可逆加密

md5、shal256

import hashlib

md5 = hashlib.md5("需要加密的資料".encode("utf-8"))

|-- b64encode()			

|-- b64decode()

asctime() # 獲取系統當前時間

ctime() # 獲取系統當前時間

time() # 獲取當前的時間戳

localtime() # 返回當前時間,以類似於元組的物件

t = time.localtime()

print(「當前時間是%s-%s-%s %s:%s:%s」 %(t.tm_year, t.tm_mon, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec))

time.strftime() # 將時間物件格式化成字串

time.strftime("%y-%m-%d %h:%m:%s", time.localtime())

time.strptime() # 時間字串轉換為時間物件

time.strptime(『2019/09/18 21:02:44』, 「%y/%m/%d %h:%m:%s」)

時間戳(timestamp) :通常來說,時間戳表示的是從2023年1月1日00:00:00開始按秒計算的偏移量。我們執行「type(time.time())」,返回的是float型別。

格式化的時間字串

元組(struct_time) :struct_time元組共有9個元素共九個元素:(年,月,日,時,分,秒,一年中第幾周,一年中第幾天,夏令時)

print(「睡眠5s」)

#sleep() #執行緒推遲自定的時間執行,單位為秒

#clock()

#這個需要注意啦,在不同的系統上含義不同,在unix系統上,它返回的是』程序時間』,用秒表示的浮點數(時間戳)

#在windows中,第一次呼叫,返回的是程序執行的實際時間,而第二次之後的呼叫是自第一次呼叫後到現在的執行

#的時間,即兩次的時間差 返回當前時間的時間戳(以秒計算,從2023年1月1日00:00:00開始到現在的時間差)

print(time.time()) # 結果:1537428578.9319177

將乙個時間戳轉換為當前時區

t = time.localtime()

print(t)

time.struct_time(tm_year=2018, tm_mon=9, tm_mday=20, tm_hour=15, tm_min=33, tm_sec=9, tm_wday=3, tm_yday=263, tm_isdst=0)

year = t.tm_year

month = t.tm_mon

print(year) #2018

gmtime()方法是將乙個時間戳轉換為utc時區(0時區)的struct_time。

print(time.gmtime())

time.struct_time(tm_year=2018, tm_mon=9, tm_mday=20, tm_hour=7, tm_min=37, tm_sec=46, tm_wday=3, tm_yday=263, tm_isdst=0)

print(time.gmtime(time.time()-9000))

time.struct_time(tm_year=2018, tm_mon=9, tm_mday=20, tm_hour=5, tm_min=17, tm_sec=10, tm_wday=3, tm_yday=263, tm_isdst=0)

print(time.mktime(time.localtime())) # 結果1537429976.0

print(time.strftime("%y-%m-%d %x",time.localtime())) #結果 2018-09-20 15:58:28

print(time.strptime(「2016:12:26:12:34:33」,"%y:%m:%d:%x"))

#結果time.struct_time(tm_year=2016, tm_mon=12, tm_mday=26, tm_hour=12, tm_min=34, tm_sec=33, tm_wday=0, tm_yday=361, tm_isdst=-1)

print(time.asctime()) #thu sep 20 16:04:16 2018 把結構換時間轉換成固定的字串表示式

print(time.ctime()) # thu sep 20 16:04:16 2018 把時間戳時間轉換成固定的字串表示式

|-- datetime.datetime.now()	# 獲取系統當前時間

論python常見內建模組

論python常見內建模組 1.系統的內建模組 syshashlib hmac sys模組 sys.ar 在python指令碼傳參使用 sys.exit 系統退出 sys.getdefaultencoding 獲取系統預設編碼 getfilesystemencoding 獲取檔案編碼 getrecu...

論python常見內建模組

sys hashlib hmac base64 time datetime 1 sys模組 sys.ar 在python指令碼傳參使用 非常重要 sys.exit 系統退出 sys.getdefaultencoding 獲取系統預設編碼 getfilesystemencoding 獲取檔案編碼 ge...

論python常見內建模組

sys.ar 在python指令碼傳參使用 sys.exit 系統退出 sys.getdefaultencoding 獲取系統預設編碼 getfilesystemencoding 獲取檔案編碼 getrecursionlimit 獲取系統預設遞迴的最大層數 setrecursionlimit num...