python 自動化之路 day 06

來源:互聯網
上載者:User

標籤:clock   contain   class   ring   dex   with   util   ted   mtime   

ATM作業講解:

 

資料訪問層

商務邏輯層

 

time & datetime模組
 1 import time 2  3  4 # print(time.clock()) #返回處理器時間,3.3開始已廢棄 , 改成了time.process_time()測量處理器運算時間,不包括sleep時間,不穩定,mac上測不出來 5 # print(time.altzone)  #返回與utc時間的時間差,以秒計算\ 6 # print(time.asctime()) #返回時間格式"Fri Aug 19 11:14:16 2016", 7 # print(time.localtime()) #返回本地時間 的struct time對象格式 8 # print(time.gmtime(time.time()-800000)) #返回utc時間的struc時間對象格式 9 10 # print(time.asctime(time.localtime())) #返回時間格式"Fri Aug 19 11:14:16 2016",11 #print(time.ctime()) #返回Fri Aug 19 12:38:29 2016 格式, 同上12 13 14 15 # 日期文字 轉成  時間戳記16 # string_2_struct = time.strptime("2016/05/22","%Y/%m/%d") #將 日期文字 轉成 struct時間對象格式17 # print(string_2_struct)18 # #19 # struct_2_stamp = time.mktime(string_2_struct) #將struct時間對象轉成時間戳記20 # print(struct_2_stamp)21 22 23 24 #將時間戳記轉為字串格式25 # print(time.gmtime(time.time()-86640)) #將utc時間戳記轉換成struct_time格式26 # print(time.strftime("%Y-%m-%d %H:%M:%S",time.gmtime()) ) #將utc struct_time格式轉成指定的字串格式27 28 29 30 31 32 #時間加減33 import datetime34 35 # print(datetime.datetime.now()) #返回 2016-08-19 12:47:03.94192536 #print(datetime.date.fromtimestamp(time.time()) )  # 時間戳記直接轉成日期格式 2016-08-1937 # print(datetime.datetime.now() )38 # print(datetime.datetime.now() + datetime.timedelta(3)) #目前時間+3天39 # print(datetime.datetime.now() + datetime.timedelta(-3)) #目前時間-3天40 # print(datetime.datetime.now() + datetime.timedelta(hours=3)) #目前時間+3小時41 # print(datetime.datetime.now() + datetime.timedelta(minutes=30)) #目前時間+30分42 43 44 #45 # c_time  = datetime.datetime.now()46 # print(c_time.replace(minute=3,hour=2)) #時間替換
View Code

 

 
Directive Meaning Notes
%a Locale’s abbreviated weekday name.  
%A Locale’s full weekday name.  
%b Locale’s abbreviated month name.  
%B Locale’s full month name.  
%c Locale’s appropriate date and time representation.  
%d Day of the month as a decimal number [01,31].  
%H Hour (24-hour clock) as a decimal number [00,23].  
%I Hour (12-hour clock) as a decimal number [01,12].  
%j Day of the year as a decimal number [001,366].  
%m Month as a decimal number [01,12].  
%M Minute as a decimal number [00,59].  
%p Locale’s equivalent of either AM or PM. (1)
%S Second as a decimal number [00,61]. (2)
%U Week number of the year (Sunday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Sunday are considered to be in week 0. (3)
%w Weekday as a decimal number [0(Sunday),6].  
%W Week number of the year (Monday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Monday are considered to be in week 0. (3)
%x Locale’s appropriate date representation.  
%X Locale’s appropriate time representation.  
%y Year without century as a decimal number [00,99].  
%Y Year with century as a decimal number.  
%z Time zone offset indicating a positive or negative time difference from UTC/GMT of the form +HHMM or -HHMM, where H represents decimal hour digits and M represents decimal minute digits [-23:59, +23:59].  
%Z Time zone name (no characters if no time zone exists).  
%% A literal ‘%‘ character.

 

random模組

隨機數

1 import random2 print(random.random())3 print(random.randint(1,5))4 print(random.randrange(1,2))

 

產生隨機驗證碼

 

1 import random2 3 import string4 5 str_source = string.ascii_letters + string.digits6 print(‘‘.join(random.sample(str_source,7)))

 

12345678910 import randomcheckcode = ‘‘for 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

 

python 自動化之路 day 06

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.