模块: time: 时间戳 --> 结构化 localtime 结构化 --> 时间戳 mktime 结构化 --> 字符串 strftime 字符串 --> 结构化 strptime time.time() # 获取当前时间戳 strftime() # 获取当前字符串时间
strftime('格式','结构化时间') # 格式可以少写 strptime('字符串时间','格式') # 格式一一对应
datetime,timedelta 时间对象 -- 时间戳 时间戳 -- 时间对象 字符串 -- 时间对象 时间对象 -- 字符串 时间对象 - timedelta(days=1)
1. time 内置的 import time 不需要咱们自己安装的
import time print(time.time()) # 浮点型(小数) 给计算机看 计算 三种: 时间戳 time.tiem 结构化时间 修改 字符串时间 给人看的
print(time.localtime()) 命名元组 :time.struct_time(tm_year=2019, tm_mon=3, tm_mday=20, tm_hour=10, tm_min=21, tm_sec=36, tm_wday=2, tm_yday=79, tm_isdst=0)
print(time.strftime('%Y-%m-%d %X')) print(time.strftime('%Y-%m-%d %H:%M:%S')) 字符串时间
1.先转换成结构化时间 2.结构化转成字符串
print(time.localtime(time.time())) f = time.localtime(time.time() - 86000 * 3) # 150000000 秒 86000 print(time.strftime('%Y-%m-%d %H:%M:%S',f)) print(time.strftime('%Y-%m-%d %X',f))
'2018-11-30 12:30'
print(type(time.strftime('%Y-%m-%d %X')))
f = time.strptime('2018-11-30 12:30','%Y-%m-%d %H:%M') # 支持索引 和.点的方法 new_time = time.mktime(f) 2*3600 new_t = time.localtime(new_time) print(time.strftime('%Y-%m-%d %H:%M:%S',new_t))
时间戳 -- 结构化时间 结构化时间 -- 字符串时间
字符串时间 -- 结构化时间 结构化时间 -- 时间戳 f = time.localtime() print(time.strftime('%X %Y-%m-%d',f))
t = '2019-03-20 10:40:00' 这个时间向后退一个月 # 1.转成结构化 f = time.strptime(t,'%Y-%m-%d %X') # 2.结构化时间转成时间戳 ts = time.mktime(f) # 3.将时间戳向大变 new_ts = ts 86400 * 30 # 4.将最新的时间戳转成结构化时间 new_f = time.localtime(new_ts) # 5.将结构化时间转成字符串时间
print(time.strftime('%Y-%m-%d %X',new_f))
获取当前时间求前一月的现在时间 1.获取时间戳进行减法计算 new_ts = time.time() - 30*86400 # 2.最新的时间戳转成结构化时间 new_f = time.localtime(new_ts) # 3.将结构化时间转成字符串时间 print(time.strftime('%Y-%m-%d %X',new_f))
time.strftime()
import datetime.datetime
print(datetime.now()) #2019-03-20 11:35:25.(471359)毫秒 时间对象
f = datetime.timestamp(datetime.now()) # 将时间对象转换成时间戳 print(datetime.fromtimestamp(f)) # 将时间戳转成时间对象
print(datetime.strptime('2018-11-30','%Y-%m-%d')) # 将字符串转成时间对象 f = datetime.now() print(datetime.strftime(f,'%Y-%m-%d')) 将时间对象转成字符串
from datetime import datetime,timedelta # 从xx导入 建议 print(datetime.now() - timedelta()) 这个是datetime的精华
collections: deque: 双端队列 两头都可添加也可以删除 队列 先进先出 栈 先进后出 Counter: 计数 计数 统计元素出现次数,以字典形式显示
计算:
c = Counter('adfasdfasdfasdfasdfasdf')
print(c)
Counter({1:2}) 这种咱们可以直接dict()转换 defaultdict: 默认字典 利用他的特性:来实现一些非常简单的操作 namedtuple: 命名元组 作用:将元组中的元素进行标明(让别人知道这元素是什么意思)
from collections import deque
双端队列
d = deque([1,2,3,4])
d.append(5) #右边添加
print(d)
d.appendleft(10) # 左边添加
print(d)
d.insert(2,99)
print(d)
d.remove(3)
print(d)
print(d.pop()) #右边删除
print(d)
print(d.popleft()) #左边删除
print(d)
来源:http://www./content-4-144401.html
|