从零开始的Linux运维屌丝之路,资源免费分享平台   运维人员首选:简单、易用、高效、安全、稳定、社区活跃的开源软件
  • 首页
  • Python
  • 31、time模块 datetime模块 random模块 string模块

31、time模块 datetime模块 random模块 string模块

发布:蔺要红05-17分类: Python



time模块
 
>>> import time       #导入时间模块
>>> time.time         #time模块为内置模块
<built-in function time>

>>> time.time()       #显示时间戳
1558081546.7464879

>>> time.localtime()  #
time.struct_time(tm_year=2019, tm_mon=5, tm_mday=17, tm_hour=16, tm_min=26, tm_sec=39, tm_wday=4, tm_yday=137, tm_isdst=0)
>>> a = time.localtime()
>>> '%s-%s-%s'%(a.tm_year,a.tm_mon,a.tm_mday)
'2019-5-17'

>>> time.gmtime() #转换成UTC时区
time.struct_time(tm_year=2019, tm_mon=5, tm_mday=17, tm_hour=8, tm_min=32, tm_sec=22, tm_wday=4, tm_yday=137, tm_isdst=0)


>>> a = time.localtime()
>>> a
time.struct_time(tm_year=2019, tm_mon=5, tm_mday=17, tm_hour=16, tm_min=34, tm_sec=49, tm_wday=4, tm_yday=137, tm_isdst=0)
>>> time.mktime(a)  #转化成时间戳
1558082089.0

>>> time.sleep(5) #等待多久时间往下进行

>>> time.asctime() 
'Fri May 17 16:37:49 2019'

>>> time.ctime() #不跟参数相当于 time.asctime() 
'Fri May 17 16:41:08 2019'
>>> time.ctime(1456812312) #把时间戳转换成时间
'Tue Mar  1 14:05:12 2016'

>>> time.strftime('%Y-%m-%d %H:%M:%S') #时间显示格式
'2019-05-17 17:33:07'
>>> time.strftime('%Y-%m-%d')  #时间显示格式
'2019-05-17'
>>> time.strftime('%Y-%m-%d %X')  #时间显示格式
'2019-05-17 16:49:09'
>>> time.strftime('%x %X')        #时间显示格式
'05/17/19 16:49:14'
>>> time.strftime('%Y-%m-%d %H:%M')     #时间显示格式
'2019-05-17 16:49'

>>> time.strftime('%Y-%m-%d %H:%M',time.localtime())   #时间显示格式
'2019-05-17 16:52'


>>> a
time.struct_time(tm_year=2019, tm_mon=5, tm_mday=17, tm_hour=16, tm_min=34, tm_sec=49, tm_wday=4, tm_yday=137, tm_isdst=0)
>>> time.strftime('%x %X',a)
'05/17/19 16:34:49'


时间戳和格式时间相互转换
 
>>> time.strftime('%Y-%m-%d %H:%M:%S') 
'2019-05-17 17:36:08'
>>> s = time.strftime('%Y-%m-%d %H:%M:%S')
>>> s
'2019-05-17 17:36:33'
>>> time.strptime(s,'%Y-%m-%d %H:%M:%S') #转换格式
time.struct_time(tm_year=2019, tm_mon=5, tm_mday=17, tm_hour=17, tm_min=36, tm_sec=33, tm_wday=4, tm_yday=137, tm_isdst=-1)
>>> time.mktime(time.strptime(s,'%Y-%m-%d %H:%M:%S'))  #转换格式
1558085793.0
 
>>> m
1558086210.0
>>> time.gmtime(m)
time.struct_time(tm_year=2019, tm_mon=5, tm_mday=17, tm_hour=9, tm_min=43, tm_sec=30, tm_wday=4, tm_yday=137, tm_i
>>> time.strftime('%Y-%m-%d %H:%M:%S',time.gmtime(m))
'2019-05-17 09:43:30'




datetime模块
 
 
>>> import datetime
>>> print('Date:'+str(datetime.datetime.now().year))
Date:2019
>>> print('Date:'+datetime.datetime.now().strftime('%y-%m-%d %H:%M:%S'))
Date:19-05-18 10:46:00
>>> print('Date:'+datetime.datetime.now().strftime('%F'))
Date:2019-05-18
>>> import time
>>> import datetime

>>> datetime.datetime.now()
datetime.datetime(2019, 5, 18, 9, 36, 58, 649519)
>>> time.time()
1558143424.9937108

>>> datetime.date.fromtimestamp(time.time())
datetime.date(2019, 5, 18)

#时间的运算
>>> datetime.datetime.now() -datetime.timedelta(days=3) #往前3天
datetime.datetime(2019, 5, 15, 9, 42, 40, 674176)

>>> datetime.datetime.now() -datetime.timedelta(minutes=10) #往前10分钟
datetime.datetime(2019, 5, 18, 9, 35, 54, 176154)

>>> datetime.datetime.now() -datetime.timedelta(seconds=10) #往前10秒
datetime.datetime(2019, 5, 18, 9, 35, 54, 176154)

>>> datetime.datetime.now() -datetime.timedelta(days=-3) #往后3天
datetime.datetime(2019, 5, 21, 9, 43, 27, 865988)
>>> datetime.datetime.now() -datetime.timedelta(hours=-3) #往后3小时
datetime.datetime(2019, 5, 18, 12, 43, 34, 601947)

#时间转换
>>> datetime.datetime.now()
datetime.datetime(2019, 5, 18, 9, 47, 46, 991323)
>>> d = datetime.datetime.now()   
>>> d.replace(year=2018)
datetime.datetime(2018, 5, 18, 9, 47, 55, 351061)
>>> d.replace(year=2018,month=12,day=11)
datetime.datetime(2018, 12, 11, 9, 47, 55, 351061)


random模块 string模块    
 
>>> import random
>>> random.randint(1,100) #获取1-100之前的一个随机数,可以包含100
97
>>> random.randint(1,100) #获取1-100之前的一个随机数,可以包含100
7
>>> random.randrange(1,100) #获取1-100之前的一个随机数,但不包含100
99
>>> random.randrange(1,100)  #获取1-100之前的一个随机数,但不包含100
76

>>> random.randrange(0,100,2)  #随机选取0-100之间的偶数
86
>>> random.randrange(1,100,2) #随机选取0-100之间的奇数
47

>>> random.random() #随机取浮点数
0.5267212650297048

>>> random.choice('asdfasdf#$71231!') #随机从字符串里获取一个值(验证码)
'7'
>>> random.sample('asdfFA#$7-231!',3)  #随机从字符串里获取3个值
['#', '$', 'F']
>>> random.sample('1234567890',4) #随机从字符串里获取4个值
['6', '0', '5', '9']


>>> import string
>>> string.digits  #随机数字
'0123456789'
>>> string.ascii_letters #随机大小写字符串
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
>>> string.ascii_lowercase  #随机小写字符串
'abcdefghijklmnopqrstuvwxyz'
>>> string.punctuation   #随机特殊字符
'!"#$%&\'()*+,-./:;<=>[email protected][\\]^_`{|}~'
>>> string.ascii_lowercase + string.digits + string.punctuation  #如上加起来随机
'abcdefghijklmnopqrstuvwxyz0123456789!"#$%&\'()*+,-./:;<=>[email protected][\\]^_`{|}~'
#利用random随机取值
>>> a = string.ascii_lowercase + string.digits
>>> a
'abcdefghijklmnopqrstuvwxyz0123456789'
>>> random.sample(a,8)
['8', '6', 'c', 'z', 'n', 'b', 'e', 'w']
>>> ''.join(random.sample(a,8))
'5vxo7n6b'

>>> a = list(range(10))
>>> a
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> random.shuffle(a) #打乱顺序
>>> a
[0, 3, 1, 7, 4, 2, 8, 6, 5, 9]


 
温馨提示如有转载或引用以上内容之必要,敬请将本文链接作为出处标注,如有侵权我会在24小时之内删除!

欢迎使用手机扫描访问本站