Day 22 常用模块

### 时间模块

python中三种表达时间的方式

* 时间戳(timestamp) :通常来说,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量(给计算机看的)
* 格式化的时间字符串(Format String): ‘1999-12-06’(给人看的)
* 结构化时间(struct_time) :struct_time元组共有9个元素共九个元素:(年,月,日,时,分,秒,一年中第几周,一年中第几天等)

```python
#导入时间模块
import time

#1.时间戳
print(time.time())
1500875844.800804

#2.时间字符串(参考博客中罗列的日期拼接格式)
print(time.strftime("%Y-%m-%d %X"))
'2019-07-24 13:54:37'
print(time.strftime("%Y-%m-%d %H-%M-%S"))
'2019-07-24 13-55-04'

print(time.strftime("%Y-%m %H-%M-%S"))
'2019-07 13-55-04'
print(time.strftime("%Y/%m/%d %X"))
'2019/07/24 13:54:37'

#3.时间元组:localtime将一个时间戳转换为当前时区的struct_time
time.localtime() # time.struct_time(tm_year=2019, tm_mon=7, tm_mday=1, tm_hour=19, tm_min=35, tm_sec=39, tm_wday=0, tm_yday=182, tm_isdst=0)

time.struct_time(tm_year=2017, tm_mon=7, tm_mday=24,
          tm_hour=13, tm_min=59, tm_sec=37,
tm_wday=0, tm_yday=205, tm_isdst=0)
```

##### 时间转换

```python
# 1.时间戳与结构化时间互相转换
>>>time.gmtime(1500000000)
time.struct_time(tm_year=2017, tm_mon=7, tm_mday=14, tm_hour=2, tm_min=40, tm_sec=0, tm_wday=4, tm_yday=195, tm_isdst=0)
>>>time.localtime(1500000000)
time.struct_time(tm_year=2017, tm_mon=7, tm_mday=14, tm_hour=10, tm_min=40, tm_sec=0, tm_wday=4, tm_yday=195, tm_isdst=0)
#结构化时间-->时间戳 
>>>time_tuple = time.localtime(1500000000) #time.mktime(结构化时间)
>>>time.mktime(time_tuple)
1500000000.0

# 2.结构化时间与格式化时间字符串
>>>time.strftime("%Y-%m-%d",time.localtime(1500000000))
'2017-07-14'
#time.strftime("格式定义","结构化时间") 结构化时间参数若不传,则显示当前时间
>>>time.strftime("%Y-%m-%d %X")
'2017-07-24 14:55:36'

#字符串时间-->结构化时间
>>>time.strptime("2017-03-16","%Y-%m-%d") #time.strptime(时间字符串,字符串对应格式)
time.struct_time(tm_year=2017, tm_mon=3, tm_mday=16, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=75, tm_isdst=-1)
>>>time.strptime("07/24/2017","%m/%d/%Y")
time.struct_time(tm_year=2017, tm_mon=7, tm_mday=24, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=0, tm_yday=205, tm_isdst=-1)
```

#### datetime模块

```python
import datetime

# 自定义日期
res = datetime.date(2019, 7, 15)
print(res) # 2019-07-15

# 获取本地时间
# 年月日
now_date = datetime.date.today()
print(now_date) # 2019-07-01
# 年月日时分秒
now_time = datetime.datetime.today()
print(now_time) # 2019-07-01 17:46:08.214170

# 无论是年月日,还是年月日时分秒对象都可以调用以下方法获取针对性的数据
# 以datetime对象举例
print(now_time.year) # 获取年份2019
print(now_time.month) # 获取月份7
print(now_time.day) # 获取日1
print(now_time.weekday()) # 获取星期(weekday星期是0-6) 0表示周一
print(now_time.isoweekday()) # 获取星期(weekday星期是1-7) 1表示周一

# timedelta对象
# 可以对时间进行运算操作
import datetime

# 获得本地日期 年月日
tday = datetime.date.today()
# 定义操作时间 day=7 也就是可以对另一个时间对象加7天或者减少7点
tdelta = datetime.timedelta(days=7)

# 打印今天的日期
print('今天的日期:{}'.format(tday)) # 2019-07-01
# 打印七天后的日期
print('从今天向后推7天:{}'.format(tday + tdelta)) # 2019-07-08
# 总结:日期对象与timedelta之间的关系
"""
日期对象 = 日期对象 +/- timedelta对象
timedelta对象 = 日期对象 +/- 日期对象

验证:

"""
# 定义日期对象
now_date1 = datetime.date.today()
# 定义timedelta对象
lta = datetime.timedelta(days=6)
now_date2 = now_date1 + lta # 日期对象 = 日期对象 +/- timedelta对象
print(type(now_date2)) # <class 'datetime.date'>
lta2 = now_date1 - now_date2 # timedelta对象 = 日期对象 +/- 日期对象
print(type(lta2)) # <class 'datetime.timedelta'>


# 小练习 计算今天距离今年过生日还有多少天
birthday = datetime.date(2019, 12, 21)
now_date = datetime.date.today()
days = birthday - now_date
print('生日:{}'.format(birthday))
print('今天的日期:{}'.format(tday))
print('距离生日还有{}天'.format(days))


# 总结年月日时分秒及时区问题
import datetime

dt_today = datetime.datetime.today()
dt_now = datetime.datetime.now()
dt_utcnow = datetime.datetime.utcnow() # UTC时间与我们的北京时间cha ju

print(dt_today)
print(dt_now)
print(dt_utcnow)
```

### random模块

参考博客内容

掌握内容:打印随机验证码

```python
import random

code = ''
for i in range(5):
str1 = str(random.randint(1,9))
str2 = chr(random.randint(65,90))
str3 = chr(random.randint(97,122))
tmp = random.choice([str1,str2,str3])
code += tmp
print(code)
```

### os模块

与操作系统打交道的模块

参考博客内容依次演示即可

### sys模块

与python解释器打交道的模块

博客:https://www.cnblogs.com/linhaifeng/p/7278389.html

猜你喜欢

转载自www.cnblogs.com/AaronY/p/12602427.html
今日推荐