1. 程式人生 > >模塊學習

模塊學習

cal one check localtime 包括 zone minutes truct del

1.time & datetime模塊

 1 #_*_coding:utf-8_*_
 2 __author__ = Alex Li
 3 
 4 import time
 5 
 6 
 7 # print(time.clock()) #返回處理器時間,3.3開始已廢棄 , 改成了time.process_time()測量處理器運算時間,不包括sleep時間,不穩定,mac上測不出來
 8 # print(time.altzone)  #返回與utc時間的時間差,以秒計算\
 9 # print(time.asctime()) #返回時間格式"Fri Aug 19 11:14:16 2016",
10 # print(time.localtime()) #返回本地時間 的struct time對象格式 11 # print(time.gmtime(time.time()-800000)) #返回utc時間的struc時間對象格式 12 13 # print(time.asctime(time.localtime())) #返回時間格式"Fri Aug 19 11:14:16 2016", 14 #print(time.ctime()) #返回Fri Aug 19 12:38:29 2016 格式, 同上 15 16 17 18 # 日期字符串 轉成 時間戳 19 # string_2_struct = time.strptime("2016/05/22","%Y/%m/%d") #將 日期字符串 轉成 struct時間對象格式
20 # print(string_2_struct) 21 # # 22 # struct_2_stamp = time.mktime(string_2_struct) #將struct時間對象轉成時間戳 23 # print(struct_2_stamp) 24 25 26 27 #將時間戳轉為字符串格式 28 # print(time.gmtime(time.time()-86640)) #將utc時間戳轉換成struct_time格式 29 # print(time.strftime("%Y-%m-%d %H:%M:%S",time.gmtime()) ) #將utc struct_time格式轉成指定的字符串格式
30 31 32 33 34 35 #時間加減
import time

t = time.strptime("2016-11-11 23:00","%Y-%m-%d %H:%M")
t1_str = time.strftime("%Y-%m-%d")
print (t1_str)

36 import datetime 37 38 # print(datetime.datetime.now()) #返回 2016-08-19 12:47:03.941925 39 #print(datetime.date.fromtimestamp(time.time()) ) # 時間戳直接轉成日期格式 2016-08-19 40 # print(datetime.datetime.now() ) 41 # print(datetime.datetime.now() + datetime.timedelta(3)) #當前時間+3天 42 # print(datetime.datetime.now() + datetime.timedelta(-3)) #當前時間-3天 43 # print(datetime.datetime.now() + datetime.timedelta(hours=3)) #當前時間+3小時 44 # print(datetime.datetime.now() + datetime.timedelta(minutes=30)) #當前時間+30分 45 46 47 # 48 # c_time = datetime.datetime.now() 49 # print(c_time.replace(minute=3,hour=2)) #時間替換
 

2.random 模塊

import random
import string
print (random.randint(1,5))
print (random.randrange(1,5))

check_code =‘‘
for i in range (6):
    current = random.randrange(0,6)
    if current != i:
        temp = chr(random.randint(65,90))
    else:
        temp = random.randint(0,9)
    check_code += str(temp)
print(check_code)

string 模塊 str_source
= string.ascii_letters+string.digits print (‘‘.join(random.sample(str_source,6)))

模塊學習