Python序列化json和pickple

知识点一:序列化与反序列化(json和pickple)

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

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

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

    pickle
        优点:
            可以支持/对应所有python的数据类型
        缺点:
            只能被python识别,不能跨平台
   
 json:
  序列化json.dump():是将字典转换成字符串类型,写入文件
  反序列化json.load():是将文件中的字符串类型,转换为字典类型读出
  
 pickle:
  序列化pickle.dump():是将字典转换成bytes类型,写入文件
  序列化pickle.load():是将从文件中将bytes类型,转换为字典类型读出
  
  


实验:
json序列化:内存中的数据类型------>中间格式json
1.序列化得到json_str (dumps接收1个参数)
json_str=json.dumps(user_info)
2.把json_str写入文件
with open('db.json','w',encoding='utf-8') as f:
    f.write(json_str)
1.2合并(用dump()接收2个参数)
with open('da.json','w',encoding='utf-8') as f:
    json.dump(user_info,f)  #文件的输出结果双引号 {"name": "yangzhizong", "age": 27}
   

总结:json格式不能识别单引号,全都是双引号
 转换后单引号默认都被转成了双引号

json的反序列化:
import json
1.从文件中读取db.json
with open('db.json','r',encoding='utf-8') as f:
   user_info=f.read()      #str类型
2.将文件转换成内存中的数据类型
user_info=json.loads(user_info)      #反序列后变为dict字典类型

1和2可以合并
with open('db.json','r',encoding='utf-8') as f:
    db_json=json.load(f)
    print(db_json)  # 反序后变为dict类型 

pickle的序列化:
import pickle
name={'name':'yangzhizong','age':27}
1.序列化得到name
name=pickle.dumps(name)                  重点注意dumps(1个参数)               
print(name,type(name))   #name被转换成bytes类型 ,写入是应该用wb
2.把name写入文件
with open('db.pickle','wb',) as f:
    f.write(name)   #输出结果是二进制
1.2合并 (用dump()接收2个参数)                    重点注意dump(2个参数)
with open('db.pickle','wb',) as f:
    pickle.dump(name,f)  


pickle的反序列化
import pickle
with open('db.pickle','rb') as f:
    info=pickle.load(f)
    print(info,type(info))   #输出结果为dict字典
  

 
知识点二:模块
1)time和datetime
import time,datetime
 1.时间戳
 start= time.time()
 time.sleep(3)
 stop= time.time()
 print(stop - start)  #3.0001089572906494

 2.格式化的字符串形式
 显示格式:
 2018:06:20 16:23:22
 2018:06:20 16:23:22 PM
 print(time.strftime('%Y:%m:%d %X'))
 print(time.strftime('%Y:%m:%d %H:%M:%S %p ')) #%p表示上午和下午

 
 
 3.结构化的时间/时间对象
 显示格式:time.struct_time(tm_year=2018, tm_mon=6, ...)
 t1=time.localtime()
 print(t1)
 print(t1.tm_year)  #2018
 结果:time.struct_time(tm_year=2018, tm_mon=6, tm_mday=20, tm_hour=16, tm_min=28, tm_sec=48, tm_wday=2, tm_yday=171, tm_isdst=0)
  
 t1=time.gmtime() (是世界标准时间格式,会有时差的区别)
 print(t1)
 结果:time.struct_time(tm_year=2018, tm_mon=6, tm_mday=20, tm_hour=8, tm_min=28, tm_sec=48, tm_wday=2, tm_yday=171, tm_isdst=0)
 
 


 4.转换形式
 结构化时间与时间戳:
 显示格式:
 time.struct_time(tm_year=1970, tm_mon=1, tm_mday=15, tm_hour=14, tm_min=56, tm_sec=7, tm_wday=3, tm_yday=15, tm_isdst=0)
 time.struct_time(tm_year=1970, tm_mon=1, tm_mday=15, tm_hour=6, tm_min=56, tm_sec=7, tm_wday=3, tm_yday=15, tm_isdst=0)
 print(time.localtime(1234567))
 print(time.gmtime(1234567))
 
 
 
 结构化时间与格式化字符串时间
 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'))


 # 获取格式化字符串形式的时间麻烦
 # 时间戳与格式化时间之间的转换麻烦
 # 获取之前或者未来的时间麻烦
 
 
 
 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))

猜你喜欢

转载自www.cnblogs.com/yangzhizong/p/9204542.html
今日推荐