# time&异常&import

### 一、高阶函数

#### 1.1 filter 过滤

l = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
# 判断一个元素是否为偶数
def is_odd(x):
if x % 2 == 0:
return True

f_l = filter(is_odd,l

f_l = filter(lambda x:x%2==1,l)

courses = ["java","Py","Groovy"]
def low(x):
return x.islower()
c_l = filter(low,courses)
print(list(c_l))


#### 1.2 reduce 聚合

from functools import reduce
nums = [1,2,3,4,5,6,7,8]
multi = 1
multiResult = reduce(lambda x,y:x*y,nums)
print(multiResult)


### 二、时间相关模块

time: 时间

datetime: 日期

calendar: 日历

#### 2.2 time

UTC:格林威治天文时间, 世界标准时间, UTC+8

DST:夏令时

import time
# 获取当前时间,时间戳,以s为单位
c = time.time()
# print(c)
# 将时间戳转换为UTC时间
g = time.gmtime(c)
print(g)
# 将时间戳转换成本地时间
l = time.localtime(c)
print(l)
# 将UTC时间转换成时间戳
c2 = time.mktime(l)
print(c2)
c3 = time.mktime((2019,12,17,20,59,59,1,351,0))
print(c3)
# 将时间戳可视化
c4 = time.ctime(c3)
print(c4)
# 将元组格式化字符串
s = time.strftime("%Y:%m:%d %H:%M:%S",l)
print(s)
# 将格式化时间字符串转换成时间元组
p = time.strptime("2019-12-17 21:07:50","%Y-%m-%d %H:%M:%S")
print(p)
#　休眠暂停
time.sleep(5)


#### 2.3 datatime 日期模块

import datetime
# 获取当前时间
d1 = datetime.datetime.now()
print(d1)

# 将指定时间转换成字符串
d4 = d1.strftime("%Y-%m-%d")
print(d4)
# 根据元组获取指定时间
d2 = datetime.datetime(2019,12,17,21,19,59,124213)
print(d2)
# 把字符串转换成时间
d5 = datetime.datetime.strptime(d4,"%Y-%m-%d")
print(d5)
# 获取时间间隔
d6 = datetime.datetime(2019,12,15,21,23,59,8888)
d7 = datetime.datetime(2019,12,17,21,22,59,9999)
d8 = d7 - d6
print(d8,d8.days)


#### 2.4 calendar 日历模块

import calendar
# 获取指定年和月日历形式
print(calendar.month(2019,12))
# 二维元组列表形式展现
print(calendar.monthcalendar(2019,12))
# 查看一年的日历
print(calendar.calendar(2019))

# 判断是否为闰年
print(calendar.isleap(2018))
# 年份区间段里, 有几个闰年
print(calendar.leapdays(2000,2020))

# 指定月份的天数，这个月第一天周几?
print(calendar.monthrange(2019,12))
# 判断当前日期星期数
print(calendar.weekday(2019,12,17))


import urllib.request.urlopen
# 通过包名和模块名[pkg.module.xxx]
urllib.request.urlopen(None)

from urllib.request import urlopen
urlopen(None)

from urllib.request import *
urlretrieve()