2019-03-20-day015

昨日回顾

序列化模块:

  • json -- load dump dumps loads
  • pickle -- load dump dumps loads
  • shelve -- 文件 + 字典 f = shelve.open('a')

持久化,json跨编码语言交互

随机数

  • random.random() # 0-1之间小数
  • random.randint() # 起始位置,终止位置
  • random.randrange() # 奇数,偶数
  • random.choice(iter)
  • random.choices(iter)
  • import random
  • random.sample 没有重复 指定一个k=?? 选择的数量
  • random.shuffle 洗牌

os

  • os.path.join # 路径拼接
  • os.path.abspath # 返回一个绝对路劲
  • os.path.dirname # 返回是目录的路径
  • os.path.basename # 返回文件名字
  • os.path.getsize # 获取文件大小 有坑
  • os.listdir
  • os.makedirs
  • os.rmovedirs
  • os.mkdir
  • os.rmdir
  • os.remove
  • os.rename

sys

  • sys.argv cmd交互模式下动态获取参数
  • sys.path python解释器模块查找路径
  • sys.version python解释器获取版本号
  • sys.platform 操作系统平台信息
  • sys.exit() 正常退出 0 异常 1

hashlib

  • md5,sha1,sha256,sha512
  • 对密码加密,判断一致性(不可逆,不可解)
  • 特别容易被撞库, (动态加盐)加盐

执行顺序

  1. 导入模块
  2. 选择加密方式
  3. 将要加密的内容转成字节然后加密
  4. 生成密文
import hashlib
md5 = hashlib.md5()   # 加盐

实例化一个对象

md5.update(b'wusir')
print(md5.hexdigest())
2950931a0b85c648ccf14137b4b1d66f
a5e646f24aa7314d8ba3dce048358e90
sha1,sha256,sha512
##密文越长,越安全-越慢

time

time 内置的

import time    不需要咱们自己安装的
import time
print(time.time()) # 浮点型(小数)  给计算机看 计算

三种:

  • 时间戳time.time()
  • 结构化时间 修改
  • 字符串时间 给人看的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))
= '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

6.最新的时间戳转成结构化时间

new_f = time.localtime(new_ts)

7.将结构化时间转成字符串时间

print(time.strftime('%Y-%m-%d %X',new_f))
time.strftime()

总结:

6个
  时间戳 -- 结构化   1    ****
  结构化 -- 时间戳   1    ****
  结构化 -- 字符串   1    ****
  字符串 -- 结构化   1    ****
  # time.time()     1 # 获取当前时间戳
  # strftime()      1 # 获取当前字符串时间
strftime('格式','结构化时间')  # 格式可以少写
strptime('字符串时间','格式')  # 格式一一对应

datetime

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导入 建议
int(datetime.now() - timedelta())
这个是datetime的精华

collections

coding:utf-8
#数据类型补充
port time
om collections import Counter
计算
c = Counter('adfasdfasdfasdfasdfasdf')
print(c)
print(dict(c))  #
om collections import namedtuple

命名元组

tu = namedtuple('juge',['name','age','sex','hobby'])
#类
t = tu('腚哥',19,'待定','抽烟,喝酒,烫头')
#实例一个对象
#明确的标明每个元素是什么意思
print(t[0])
#对象找属性
class A:
    @property
    def func(self):
        print(11)
a = A()
a.func
om 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)
#列  栈
FIFO 先进先出
     后进先出  先进后去
om collections import defaultdict

默认字典

li = [
    {'name':'alex','hobby':'抽烟'},
    {'name':'alex','hobby':'喝酒'},
    {'name':'alex','hobby':'烫头'},
    {'name':'alex','hobby':'撩妹'},
    {'name':'wusir','hobby':'小宝剑'},
    {'name':'wusir','hobby':'游泳'},
    {'name':'wusir','hobby':'打牌'},
    {'name':'太白','hobby':'烫头'},
    {'name':'太白','hobby':'洗脚'},
    {'name':'太白','hobby':'开车'},
]
for i in li:
    d[i['name']].append(i['hobby'])
print([dict(d)])
li = [('红色',1),('黄色',1),('绿色',1),('蓝色',1),('红色',5),('绿色',1),('绿色',1),('绿色',1)]
d = defaultdict(list)
for i in li:
    d[i[0]].append(i[1])
dd = dict(d)
for em in dd:
    dd[em] = sum(dd[em])
print(dd)
dic = {"1":2,'2':2,'3':5,'alex':1,'wusir':2,'eva_j':3,'renjia':4}
while 1:
    time.sleep(0.5)
    print(dic)
print dic

总结

模块:

time:

  1. 时间戳 -- 结构化
  2. 结构化 -- 时间戳
  3. 结构化 -- 字符串
  4. 字符串 -- 结构化

datetime,timedelta

  1. 时间对象 -- 时间戳
  2. 时间戳 -- 时间对象
  3. 字符串 -- 时间对象
  4. 时间对象 -- 字符串
  5. 时间对象 - timedelta(days=1)

collections:

deque: 双端队列

  1. 两头都可添加也可以删除
  2. 队列 先进先出
  3. 栈 先进后出

Counter: 计数

  1. 计数 统计元素出现次数,以字典形式显示
  2. Counter({1:2}) 这种咱们可以直接dict()转换

defaultdict: 默认字典

  1. 利用他的特性:来实现一些非常简单的操作

namedtuple: 命名元组

  1. 作用:将元组中的元素进行标明(让别人知道这元素是什么意思)

猜你喜欢

转载自www.cnblogs.com/meilong/p/20190320day015.html
今日推荐