python---內建模組

來源:互聯網
上載者:User

標籤:ima   時區   localtime   int   隨機   特定   from   logs   mtime   

時間模組

時間分為三種類型:時間戳記,結構化時間,格式化時間

#時間模組,timeimport time#時間戳記x = time.time()time.gmtime()   #將時間戳記轉換成UTC時間元組y = time.localtime()    #將時間戳記轉換成本地時區的時間元組print(y)#結構化資料,為元組的形式y = time.mktime(y)       #將結構化資料轉換成時間戳記print(y)#格式化資料z = time.strftime("%Y-%m-%d %H:%M:%S",y) #將結構化資料轉換成格式化資料#time.strftime("格式","結構化的時間資料(元組)")  --->將結構化時間資料轉化成格式化時間資料#time.strptime("格式化時間字串","格式")  ----->按著給定的格式進行匹配格式化時間字串,並轉換成格式化時間資料樣本# >>> x = time.localtime()# >>> print(x)# time.struct_time(tm_year=2017, tm_mon=7, tm_mday=9, tm_hour=23, tm_min=11, tm_se# c=32, tm_wday=6, tm_yday=190, tm_isdst=0)# >>> time.strftime("%Y-%m-%d %H:%M:%S",x)        #即%Y去匹配tm_year,%m匹配tm_mon,無須注意順序# ‘2017-07-09 23:11:32‘# >>># >>> time.strptime(‘2017-07-09 23:11:32‘,"%Y-%m-%d %H:%M:%S")    %Y 匹配2017,必須注意順序# time.struct_time(tm_year=2017, tm_mon=7, tm_mday=9, tm_hour=23, tm_min=11, tm_se# c=32, tm_wday=6, tm_yday=190, tm_isdst=-1)## time.ctime(x)   #將時間戳記資料轉換成特定格式# >>> time.ctime(x)# ‘Mon Jul 10 19:26:16 2017‘# >>> time.asctime(y) #將結構化資料轉換成特定格式# ‘Mon Jul 10 19:26:45 2017‘
時間模組

時間加減 

import datetimeprint(datetime.datetime.now()) #返回 2016-08-19 12:47:03.941925print(datetime.date.fromtimestamp(time.time()) )  # 時間戳記直接轉成日期格式 2016-08-19print(datetime.datetime.now() )print(datetime.datetime.now() + datetime.timedelta(3)) #目前時間+3天print(datetime.datetime.now() + datetime.timedelta(-3)) #目前時間-3天print(datetime.datetime.now() + datetime.timedelta(hours=3)) #目前時間+3小時print(datetime.datetime.now() + datetime.timedelta(minutes=30)) #目前時間+30分c_time  = datetime.datetime.now()print(c_time.replace(minute=3,hour=2)) #時間替換

 

時間轉換圖

 

隨機模組

 1 import random 2 # print(random.randint(1,9)) 3 # print(random.random()) 4 # 5 # print(random.randrange(2,10,4)) 6 # print(random.sample(‘chenglv‘,2)) 7 # print(random.randrange(0,99,2)) 8 # print(random.uniform(1,10)) 產生驗證碼10 checkcode = ""11 for i in range(4):12     x = random.randint(0, 9)13     y = random.choice("adbcdefglikopnm")14     if i == random.randint(0,3):15         tmp = x16     else:17         tmp = y18     checkcode += str(tmp)19 20 print(checkcode)

 

python---內建模組

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.