1.將字符串類型時(shí)間轉(zhuǎn)換成時(shí)間類型
aa='2018-01-19 00:00:00'
bb ='2018-01-19 00:01:00'
a = time.strptime(aa,'%Y-%m-%d %H:%M:%S')
b = time.strptime(bb,'%Y-%m-%d %H:%M:%S')
print type(a)? #<type 'time.struct_time'>
print a<b # True
2.隨機(jī)生成字符、特殊字符姜性、數(shù)字瞪慧,字符串
random.choice(string.ascii_letters)#隨機(jī)生成一個(gè)字母(大寫(xiě)或者小寫(xiě))
random.choice(string.punctuation)#隨機(jī)生成一個(gè)特殊字符("""!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~""")
''.join(random.choice(string.ascii_letters) for i in range(3))#隨機(jī)生成長(zhǎng)度為3的字符串(只包含大寫(xiě)或者小寫(xiě)字母)
random.randint(-10,0)#隨機(jī)生成一個(gè)負(fù)整數(shù)(-10~0)
round(random.random(),2)#隨機(jī)生成一個(gè)2位有效數(shù)的0~1的小數(shù)(-10~0).
''.join(random.choice(string.ascii_letters+string.digits) for i in range(length))#隨機(jī)生成長(zhǎng)度為length的包含字母+數(shù)字的字符串
3.根據(jù)當(dāng)前時(shí)間隨機(jī)生成一個(gè)字符串
datetime.now().strftime("%Y%m%d%H%M%S")+str(random.randint(100,999))#當(dāng)前時(shí)間(年月日時(shí)分秒)+3位隨機(jī)數(shù)
datetime.now().strftime("%H%M%S")+str(random.randint(100,999))#當(dāng)前時(shí)間(時(shí)分秒)+3位隨機(jī)數(shù)
4.獲取當(dāng)前時(shí)間,并時(shí)間字符串轉(zhuǎn)日期格式部念,日期轉(zhuǎn)字符串弃酌,獲取明天、昨天時(shí)間
from datetime import datetime,timedelta
nowTime_str= datetime.now().strftime("%Y-%m-%d")+" 00:00:00"#日期格式轉(zhuǎn)字符串
nowTime_date = datetime.strptime(nowTime_str,"%Y-%m-%d %H:%M:%S")#字符串格式轉(zhuǎn)日期
tomorrowTime = nowTime_date+timedelta(days=1)#獲取明天日期
yesterdayTime = nowTime_date-timedelta(days=1)#獲取昨天日期