標籤:ftime mtime color set span 逗號 readline exp 函數名
time
# 目前時間戳time.time()# 和標準時間(格林威治時間)的偏差time.altzone# 返回時間格式 Sat Feb 18 15:11:10 2017time.asctime()# 返回時間格式 Sat Feb 18 15:11:10 2017time.ctime()# 返回本地時間對象time.localtime() t = time.localtime() print(t.tm_year,t.tm_mon) t = time.localtime(time.time()-(60*60*24))# 返回UTC時間(格林威治時間)對象time.gmtime()time.strftime("%Y-%m-%d %H:%M:%S") 2017-02-18 15:24:25# 顯示十天前的時間struct_time = time.localtime(time.time() - 86400)time.strftime("%Y-%m-%d %H:%M:%S", struct_time)s_time = time.strptime("2017-02-17", "%Y-%d-%m")time.mktime(s_time) time.strptime() time.mktime()"2016/05/22" ----------------> 時間對象 --------------> 時間戳記
datetime
# 返回結果 2017-18 15:24:25datetime.datetime.now()# 時間戳記直接轉成日期格式datetime.datetime.fromtimestamp(time.time())datetime.datetime.now() - datetime.timedelta(hours=3,minutes=20)# 時間替換datetime.datetime.now().replace(year=2016,month=3)
random
print(random.randint(1,10))print(random.randrange(1,10)) 不包含10,可以加步長print(random.sample([1,2,3,4,5],2)) 隨機取兩個元素列表--------------------------------------------------------------------隨機驗證碼第一種import randomcheckcode=‘‘for i in range(4): current = random.randrange(0,4) if current != i: temp = chr(random.randint(65,90)) else: temp = random.randint(0,9) checkcode += str(temp)print(checkcode)第二種import random,stringsource = string.digits + string.ascii_lowercaseprint("".join(random.sample(source,6)))
os
import platformprint(platform.system()) 顯示當前作業系統os.popen(‘pwd‘).read() 可以拿到命令的執行結果os.system 只返回命令執行結果的狀態
sys
sys.argvsys.path
pickle & json
account = { ‘id‘: 62253454873, ‘credit‘: 15000, ‘balance‘: 8000, ‘expire_date‘:‘2020-05-21‘, ‘password‘: ‘fdsjkl‘}f = open(‘account.db‘,‘w‘)f.write(str(account))f.close()------------------------------f = open(‘account.db‘,‘r‘)account = eval(f.readlines())序列化 記憶體 --> 字串還原序列化 字串 --> 記憶體pickle.dumps、pickle.dump 序列化pickle.loads、pickle.load 還原序列化json 只支援str, int, float, set, dict, list, tuple資料類型
logging
日誌格式
%(name)s |
Logger的名字 |
%(levelno)s |
數字形式的記錄層級 |
%(levelname)s |
文本形式的記錄層級 |
%(pathname)s |
調用日誌輸出函數的模組的完整路徑名,可能沒有 |
%(filename)s |
調用日誌輸出函數的模組的檔案名稱 |
%(module)s |
調用日誌輸出函數的模組名 |
%(funcName)s |
調用日誌輸出函數的函數名 |
%(lineno)d |
調用日誌輸出函數的語句所在的程式碼 |
%(created)f |
目前時間,用UNIX標準的表示時間的浮 點數表示 |
%(relativeCreated)d |
輸出日誌資訊時的,自Logger建立以 來的毫秒數 |
%(asctime)s |
字串形式的目前時間。預設格式是 “2003-07-08 16:49:45,896”。逗號後面的是毫秒 |
%(thread)d |
線程ID。可能沒有 |
%(threadName)s |
線程名。可能沒有 |
%(process)d |
進程ID。可能沒有 |
%(message)s |
使用者輸出的訊息 |
python常用模組