zoukankan      html  css  js  c++  java
  • json&pickle模块、时间模块

    一、json&pickle模块

    1.序列化

    01 什么是序列化/反序列化
    序列化就是将内存中的数据结构转换成一种中间格式存储到硬盘或者基于网络传输
    发序列化就是硬盘中或者网络中传来的一种数据格式转换成内存中数据结构

    02 为什要有
    1、可以保存程序的运行状态
    2、数据的跨平台交互

    03 怎么用
    json
    优点:
    跨平台性强
    缺点:
    只能支持/对应python部分的数据类型

    pickle
    优点:
    可以支持/对应所有python的数据类型
    缺点:
    只能被python识别,不能跨平台
    2.json模块序列化与反序列化
    序列化:内存中的数据类型------>中间格式json
    dic={'name':'akko','age':16,'sex':'male'}
    # 1、序列化得到json_str
    json_str=json.dumps(dic)
    # 2、把json_str写入文件
    with open('db.json','wt',encoding='utf-8') as f:
    f.write(json_str)

    1和2合为一步
    with open('db.json','wt',encoding='utf-8') as f:
    json.dump(dic,f)
    
    
    反序列化:中间格式json----->内存中的数据类型

    #1、从文件中读取json_str
    with open('db.json','rt',encoding='utf-8') as f:
    json_str=f.read()
    #2、将json_str转成内存中的数据类型
    dic=json.loads(json_str)

    1和2可以合作一步
    with open('db.json','rt',encoding='utf-8') as f:
    dic=json.load(f)

    验证:print(dic['sex'])
    注意点:json格式不能识别单引号,全都是双引号


    3.pickle序列化
    dic={'a':1,'b':2,'c':3}

    1 序列化
    pkl=pickle.dumps(dic)
    print(pkl,type(pkl))

    2 写入文件
    with open('db.pkl','wb') as f:
    f.write(pkl)

    1和2可以合作一步
    with open('db.pkl','wb') as f:
    pickle.dump(dic,f)

    pickle反序列化
     #1、从文件中读取pickle格式
    with open('db.pkl','rb') as f:
    pkl=f.read()
    #2、将json_str转成内存中的数据类型
    dic=pickle.loads(pkl)
    print(dic['a'])

    1和2可以合作一步
    with open('db.pkl','rb') as f:
    dic=pickle.load(f)

    #验证:print(dic['a'])


    二、时间模块

    import time
    时间分为三种格式
    1、时间戳
    start= time.time()
    time.sleep(3)
    stop= time.time()
    print(stop - start)

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

    3、 结构化的时间/时间对象
    t1=time.localtime()
    print(t1)
    print(type(t1.tm_min))
    print(t1.tm_mday)

    t2=time.gmtime()
    print(t1)
    print(t2)

    时间转换
    print(time.localtime(123123123))
    print(time.gmtime(123123123))

    print(time.mktime(time.localtime()))

    print(time.strftime('%Y',time.localtime()))
    print(time.strptime('2011-03-07','%Y-%d-%m'))


    print(time.asctime())
    print(time.ctime())
    print(time.strftime('%a %b %d %H:%M:%S %Y'))

    print(time.asctime(time.localtime()))
    print(time.ctime(123123123))

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



    问题是:
    获取格式化字符串形式的时间麻烦
    时间戳与格式化时间之间的转换麻烦
    获取之前或者未来的时间麻烦



    计算机认识的时间只能是'时间戳'格式,而程序员可处理的或者说人类能看懂的时间有: '格式化的时间字符串','结构化的时间' ,于是有了下图的转换关系

    datetime 模块
    import datetime

    print(datetime.datetime.now())
    print(datetime.datetime.fromtimestamp(1231233213))

    print(datetime.datetime.now() + datetime.timedelta(days=3))
    print(datetime.datetime.now() + datetime.timedelta(days=-3))


    s=datetime.datetime.now()
    print(s.replace(year=2020))







  • 相关阅读:
    abp架构中加载公共css样式表和公共js的文件目录位置
    angular中[hidden]="expression"注意事项
    angular中使用canvas画布做验证码
    AngularJs页面跳转
    Angular学习笔记【如何正确使用第三方组件】
    【JavaScript权威指南】——逻辑与(&&)
    angular学习笔记【ng2-charts】插件添加
    OpenLayers v4.2.0 -----地图延迟加载;
    Sharepoint 图片库字段名称(Title)和对应的内部名称(InternalName)
    Sharepoint JSCOM 列表操作
  • 原文地址:https://www.cnblogs.com/kingyanan/p/9208894.html
Copyright © 2011-2022 走看看