zoukankan      html  css  js  c++  java
  • 包的使用,json与pickle模块,time与datetime模块,random模块

    '''
    1. 什么是包
    包就是一个含有__init__.py文件的文件夹

    2. 为何要用包

    3. 如何用包

    '''

    import sys
    sys.path.append(r'D:脱产5期内容day16dir')

    import aaa # aaa--------->__init__.py

    # print(aaa.xxx)



    aaa.func1()
    aaa.func2()
    aaa.func3()
    aaa.func4()
    aaa.f1()
    aaa.f2()
    aaa.f3()
    aaa.f4()
    aaa.fff()


    包内__init__.py:

    # print('__init__.py run...')

    xxx=111
    第一种绝对导入
    # from aaa.m1 import func1,func2,func3,func4
    # from aaa.m2 import f1,f2,f3,f4

    # from aaa.bbb.m3 import fff
    第二种相对导入,.只能在包内使用
    from .m1 import func1,func2,func3,func4
    from .m2 import f1,f2,f3,f4
    from .bbb.m3 import fff




    1. 什么是序列化
    序列化就是将内存中的数据类型转成另外一种格式

    即:
    字典---------序列化--------->其他的格式--------------->存到硬盘
    硬盘---读取---->其他格式----------反序列化-------->字典

    2. 为什么要序列化
    1. 持久保存程序的运行状态
    2. 数据的跨平台交互



    3. 如何序列化
    json:
    优点: 这种格式是一种通用的格式,所有编程语言都能识别
    缺点: 不能识别所有python类型
    强调:json格式不能识别单引号

    pickle
    优点: 能识别所有python类型
    缺点: 只能被python这门编程语言识别


    ========================json
    import json

    dic={'k1':True,'k2':10,'k3':'egon','k4':'你好啊'}

    # # 序列化
    dic_json=json.dumps(dic)
    print(dic_json,type(dic_json))
    #
    # # 持久化
    with open('a.json',mode='wt',encoding='utf-8') as f:
    f.write(dic_json)

    序列化+持久化
    with open('a.json',mode='wt',encoding='utf-8') as f:
    json.dump(dic,f)


    import json
    # 从文件中读取json格式化的字符
    with open('a.json',mode='rt',encoding='utf-8') as f:
    dic_json=f.read()

    # 反序列化
    dic=json.loads(dic_json)
    print(dic,dic['k1'])

    读取文件内容+反序列化
    with open('a.json',mode='rt',encoding='utf-8') as f:
    dic=json.load(f)
    print(dic['k1'])



    ========================pickle
    import pickle

    dic={'k1':True,'k2':10,'k3':'egon','k4':'你好啊',}


    ===========>1 pickle.dumps与pickle.loads

    dic_pkl=pickle.dumps({1,2,3,4})
    # print(dic_pkl)

    with open('b.pkl',mode='wb') as f:
    f.write(dic_pkl)





    with open('b.pkl',mode='rb') as f:
    s_pkl=f.read()
    s=pickle.loads(s_pkl)
    print(type(s))


    ===========>2 pickle.dump与pickle.load
    with open('c.pkl',mode='wb') as f:
    pickle.dump(dic,f)

    with open('c.pkl',mode='rb') as f:
    dic=pickle.load(f)
    print(dic,type(dic))




    时间分为三种格式:
    import time
    1. 时间戳
    print(time.time())

    2. 格式化的字符
    print(time.strftime('%Y-%m-%d %H:%M:%S %p'))

    3. 结构化的时间对象
    print(time.localtime())
    print(time.localtime().tm_hour)
    print(time.localtime().tm_wday)
    print(time.localtime().tm_yday)
    print(time.gmtime())

    时间转换
    时间戳---->struct_time------->格式化的字符串
    struct_time=time.localtime(123123)
    print(struct_time)

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

    格式化的字符串---->struct_time------->时间戳
    struct_time=time.strptime('2017-03-11','%Y-%m-%d')
    print(struct_time)

    print(time.mktime(struct_time))


    import datetime
    print(datetime.datetime.now())     当前时间直接转为格式化字符串

    print(datetime.datetime.fromtimestamp(1231231))    加秒数直接转为格式化字符串

    print(datetime.datetime.now() + datetime.timedelta(days=3))      加时间直接转为格式化字符串
    print(datetime.datetime.now() - datetime.timedelta(days=3))      
    print(datetime.datetime.now() + datetime.timedelta(days=-3))
    print(datetime.datetime.now() + datetime.timedelta(days=3,hours=3))


    random模块

    import random

    print(random.random())    (0,1) 之间的小数, float
    print(random.randint(1,3))    [ ],头和尾都能取到 int
    print(random.randrange(1,3))    [ ),顾头不顾尾 int
    print(random.uniform(1,3))    ()两数之间的小数,float

    print(random.choice([1,'a','c']))    随机取一个数
    print(random.sample([1,'a','c'],2))    随机取指定的个数(2)


    item=[1,3,5,7,9]
    random.shuffle(item)    打乱列表顺序
    print(item)

    随机验证码:

    def make_code(max_size=5):
    res=''
    for i in range(max_size):
    num=str(random.randint(0,9))
    alp=chr(random.randint(65,90))

    res+=random.choice([num,alp])

    return res


    print(make_code(10))


  • 相关阅读:
    Vue前端工程化
    Vue前端路由
    Vue前后端路由
    Vue组件化开发
    Vue基础
    订单列表和数据列表(七)
    商品列表和添加商品(六)
    商品分类管理和参数管理(五)
    给独立搭建的博客启用https的过程
    使用Gitalk实现静态页面评论的功能
  • 原文地址:https://www.cnblogs.com/huangchaonan/p/10071142.html
Copyright © 2011-2022 走看看