標籤:source 英文 .com 模組 font pytho struct code mon
1.1 時間模組time() 與 datetime()
1、 time()模組中的重要函數
函數 |
描述 |
asctime([tuple]) |
將時間元群組轉換為字串 |
localtime([secs]) |
將秒數轉換為日期元組(轉換成本國時區而不是utc時區) |
mktime(tuple) |
將時間元群組轉換為本地時間 |
sleep(secs) |
休眠(不做任何事情)secs秒 |
strptime(string[, format]) |
將字串解析為時間元組 |
time() |
擷取目前時間戳 |
time.gmtime() |
將時間轉換成utc格式的元組格式 |
2、time()模組時間格式轉換
3、time()模組時間轉換
1. 時間戳記 1970年1月1日之後的秒, 即:time.time()
2. 格式化的字串 2014-11-11 11:11, 即:time.strftime(‘%Y-%m-%d‘)
3. 結構化時間 元組包含了:年、日、星期等... time.struct_time 即:time.localtime()
import timeprint(time.time()) # 時間戳記:1511166937.2178104print(time.strftime(‘%Y-%m-%d‘)) # 格式化的字串: 2017-11-20print(time.localtime()) # 結構化時間(元組): (tm_year=2017, tm_mon=11...)print(time.gmtime()) # 將時間轉換成utc格式的元組格式: (tm_year=2017, tm_mon=11...)#1. 將結構化時間轉換成時間戳記: 1511167004.0print(time.mktime(time.localtime()))#2. 將格字串時間轉換成結構化時間 元組: (tm_year=2017, tm_mon=11...)print(time.strptime(‘2014-11-11‘, ‘%Y-%m-%d‘))#3. 結構化時間(元組) 轉換成 字串時間 :2017-11-20print(time.strftime(‘%Y-%m-%d‘, time.localtime())) # 預設目前時間#4. 將結構化時間(元組) 轉換成英文字串時間 : Mon Nov 20 16:51:28 2017print(time.asctime(time.localtime()))#5. 將時間戳記轉成 英文字串時間 : Mon Nov 20 16:51:28 2017print(time.ctime(time.time()))
time()模組時間轉換
4、ctime和asctime區別
1)ctime傳入的是以秒計時的時間戳記轉換成格式化時間
2)asctime傳入的是時間元群組轉換成格式化時間
import timet1 = time.time()print(t1) #1483495728.4734166print(time.ctime(t1)) #Wed Jan 4 10:08:48 2017t2 = time.localtime()print(t2) #time.struct_time(tm_year=2017, tm_mon=1, tm_mday=4, tm_hour=10, print(time.asctime(t2)) #Wed Jan 4 10:08:48 2017
ctime和asctime區別
5、datetime
import datetime#1、datetime.datetime擷取目前時間print(datetime.datetime.now())#2、擷取三天后的時間print(datetime.datetime.now()+datetime.timedelta(+3))#3、擷取三天前的時間print(datetime.datetime.now()+datetime.timedelta(-3))#4、擷取三個小時後的時間print(datetime.datetime.now()+datetime.timedelta(hours=3))#5、擷取三分鐘以前的時間print(datetime.datetime.now()+datetime.timedelta(minutes = -3))import datetimeprint(datetime.datetime.now()) #2017-08-18 11:25:52.618873print(datetime.datetime.now().date()) #2017-08-18print(datetime.datetime.now().strftime("%Y-%m-%d %H-%M-%S")) #2017-08-18 11-25-52datetime使用1.2 random()模組
1、random()模組常用函數
random模組中一些重要函數
函數 |
描述 |
random() |
返回0<n<=1 |
getrandbits(n) |
以長整形形式返回n個隨機位 |
uniform(a, b) |
返回隨機實數n,其中a<=n<=b |
randrange([start], stop, [step]) |
返回range(start,stop,step)中的隨機數 |
choice(seq) |
從序列seq中返回隨意元素 |
shuffle(seq[, random]) |
原地指定序列seq(將有序列表變成無序的:洗牌) |
sample(sea, n) |
從序列seq中選擇n個隨機且獨立的元素 |
import random#⒈ 隨機整數:print(random.randint(0,99)) # 隨機選取0-99之間的整數print(random.randrange(0, 101, 2)) # 隨機選取0-101之間的偶數#⒉ 隨機浮點數:print(random.random()) # 0.972654134347print(random.uniform(1, 10)) # 4.14709813772#⒊ 隨機字元:print(random.choice(‘abcdefg‘)) # cprint(random.sample(‘abcdefghij‘,3)) # [‘j‘, ‘f‘, ‘c‘]
random()函數使用舉例
2、使用random實現四位驗證碼
import randomcheckcode = ‘‘for i in range(4): current = random.randrange(0,4) if current == i: tmp = chr(random.randint(65,90)) #65,90表示所有大寫字母 else: tmp = random.randint(0,9) checkcode += str(tmp)print(checkcode) #運行結果: 851K
使用for迴圈實現
import randomimport stringstr_source = string.ascii_letters + string.digitsstr_list = random.sample(str_source,7)#[‘i‘, ‘Q‘, ‘U‘, ‘u‘, ‘A‘, ‘0‘, ‘9‘]print(str_list)str_final = ‘‘.join(str_list)#iQUuA09print(str_final) # 運行結果: jkFU2Ed
使用random.sample實現
>>> string.digits‘0123456789‘>>> string.ascii_lowercase‘abcdefghijklmnopqrstuvwxyz‘
1.3 os模組
04: python常用模組