オリジナル:https://www.cnblogs.com/yuanchenqi/article/5732581.html
時間モジュール
3つの時間表現
Pythonでは、この時間を表現するために、通常はいくつかの方法があります。
- タイムスタンプ(タイムスタンプ):一般的に言って、タイムスタンプは1970年1月から夜12時00分00秒1秒でプレスをオフセットことを示しています。私たちは、それがfloatを返し、 "タイプ(time.timeを())" を実行します。
- 時刻文字列は、フォーマット
- タプル(なstruct_time):9つの要素のタプルなstruct_timeの合計は、9つの要素でした:(年、月、日、時、分、秒、年の最初の数週間、今年の日、夏時間)
インポート時間
#1の時間は、():現在のタイムスタンプを返します
time.timeを() #1,473,525,444.037215
#---------------------------- ------------------------------
#2 LOCALTIME([秒])
#現在の時間帯するstruct_timeのタイムスタンプに変換します。提供されていないパラメータ秒は、現在の時刻は、対象を配置します。
time.localtime() #time.struct_time(tm_yearが= 2016 = tm_mon。9、tm_mday =。11、tm_hour = 0、
#tm_min = 38のtm_sec = 39、= tm_wdayコンポーネントである。6、tm_yday = 255、tm_isdstが= 0)
time.localtime (1,473,525,444.037215 )
#---------------------------------------------- ------------
#同様の3のgmtime([秒])とのlocaltime()メソッドのgmtime()メソッドタイムスタンプするstruct_timeをUTC時間帯(0時領域)を変換することです。
#-------------------------------------------------- --------
#4はmktime(T):なstruct_timeタイムスタンプの変換。
印刷(はtime.mktime(time.localtime())) #1473525749.0
#---------------------------------- ------------------------
#5いるasctime([T])。タプルは、時間によって、またはこのフォームなstruct_timeで発現として表される:「サン6月20日夜11時21分05秒1993」。
#パラメータがない場合は、パラメータとして)(time.localtimeになります。
印刷(time.asctime()) #日9月11日午後12時43分43秒2016
#------------------------------- ---------------------------
#6のctime([秒])が:(浮動小数点演算秒)のタイムスタンプとに変換しました。 time.asctime()フォーム。パラメータが与えられていないか、されていない場合は
#時間のいずれも、それがパラメータとしてtime.time()がデフォルト設定されます。それはtime.asctime(time.localtime(秒))機能します。
プリント(time.ctime()) #日に9月11日午後12時46分38秒2016
プリント(time.ctime(time.time())) #日で2016年9月午後12時46分38秒。11
#7のstrftime(フォーマット[T] ):((time.localtimeによって示されるように)時間タプルあるいはするstruct_timeを表し、
#))(time.gmtimeを返すは、時間形式文字列に変換されます。トンが指定されていない場合は、着信time.localtime()。いずれかのタプルの場合は
#の範囲外の要素、とValueErrorエラーがスローされます。
印刷(time.strftime(" %D %% Y-X-M-%"、time.localtime())) #2016年9月11日0時49分56秒
#8 time.strptime(文字列[フォーマット])
#フォーマットするstruct_timeに時間文字列。実際にはのstrftime()の逆の動作です。
印刷(time.strptime(" 2011-05-05夜04時37分06秒' ' %Y-M - %%% X-D ' ))
#time.struct_time(tm_yearが= 2011 tm_mon = 5、tm_mday = 5、tm_hour = 16、= 37 [tm_min、のtm_sec = 6、。。。
# tm_wdayコンポーネント= 3、。 = 125 tm_yday、tm_isdstが= -1)
#この機能では、フォーマットデフォルト: "%%Bの%D %H:%M:%S%Y"。
#9 SLEEP(秒)
#1 秒で、指定した時間を延期実行中のスレッド。
#10クロック()
#このノート、異なるシステムで異なる意味。UNIXシステムでは、それは秒単位での浮動小数点(タイムスタンプ)である、「時間のプロセス」を返します。
#WINDOWS、最初の呼び出しで、リターンは、プロセスの実際の実行時間です。第二は、現在の実行以来、最初の呼び出しの後に呼び出された
#の2倍の時間差である時間、。
import time
#时间戳 #计算
# print(time.time()) #1481321748.481654秒
#结构化时间---当地时间
# print(time.localtime(1531242343))
# t=time.localtime()
# print(t.tm_year)
# print(t.tm_wday)
# #-----#结构化时间---UTC
# print(time.gmtime())
#-----将结构化时间转换成时间戳
# print(time.mktime(time.localtime()))
#------将结构化时间转成字符串时间strftime
#print(time.strftime("%Y---%m-%d %X",time.localtime()))
#------将字符串时间转成结构化时间strptime
#print(time.strptime("2016:12:24:17:50:36","%Y:%m:%d:%X"))
# print(time.asctime())
# print(time.ctime())
# import datetime
# print(datetime.datetime.now())
random模块
import random
print(random.random())#(0,1)----float
print(random.randint(1,3)) #[1,3]
print(random.randrange(1,3)) #[1,3)
print(random.choice([1,'23',[4,5]]))#23
print(random.sample([1,'23',[4,5]],2))#[[4, 5], '23']
print(random.uniform(1,3))#1.927109612082716
item=[1,3,5,7,9]
random.shuffle(item)
print(item)
import random # print(random.random())#0.8882268701490094 # print(random.randint(1,3))#1到3, 2 # print(random.randrange(1,3))#1到2, 2 # print(random.choice([11,22,33,44,55]))#22 # print(random.sample([11,22,33,44,55],2))#[33, 44] # print(random.uniform(1,4))#1到4的浮点型,3.5961920452336846 ret=[1,2,3,4,5] random.shuffle(ret)#打乱 print(ret)#[4, 5, 2, 3, 1] ''' def v_code(): ret="" for i in range(5): num=random.randint(0,9) alf=chr(random.randint(65,122)) s=str(random.choice([num,alf])) ret+=s return ret print(v_code()) '''
验证码:
import random
def v_code():
code = ''
for i in range(5):
num=random.randint(0,9)
alf=chr(random.randint(65,90))
add=random.choice([num,alf])
code += str(add)
return code
print(v_code())