zoukankan      html  css  js  c++  java
  • json,pickle,shelve序列化

    import json
    
    a = [{"a":"b"}]
    jd = json.dumps(a) #序列化,就是对象通过内存能够存储和传输的过程
    with open("json.txt","w") as wo: #创建句柄
        wo.write(jd)
    import json
    
    with open("json.txt","r") as pe:
        pr = pe.read()   #读取json.txt类容
        jl = json.loads(pr)  #反序列化  把变量内容从序列化的对象重新读到内存里称之为反序列化
        print(jl)
    import json
    
    with open("json.txt","r") as pe:
        # pr = pe.read()   #读取json.txt类容
        # jl = json.loads(pr)  #反序列化  把变量内容从序列化的对象重新读到内存里称之为反序列化
        jl = json.load(pe)
        print(jl)
    import json
    
    with open("json.txt","r") as pe:
        # pr = pe.read()   #读取json.txt类容
        # jl = json.loads(pr)  #反序列化  把变量内容从序列化的对象重新读到内存里称之为反序列化
        jl = json.load(pe)
        print(jl)

     -------------------------------------------------------pickle-----------------------------------------------------------------------------------

    b = {"a":"c","g":"f"}
    pic = pickle.dumps(b)
    #创建句柄
    with open("pickle","wb") as wp:
        wp.write(pic)
    import pickle
    
    with open("pickle","rb",) as ws:
        print(pickle.loads(ws.read()))

     --------------------------------------------------------------------------------------------------------------------------

    import shelve
    
    #创建句柄
    she = shelve.open("wenjian")
    she["s"] = {"a":"b","f":"g"}  #添加内容
    #----------------------------------------------------
    she = shelve.open("wenjian")
    print(she.get("s"))   #读取信息
    
    #----------------------------------------------------
    b = {"a":"b","c":"d"}
    print(b.get("a"))
    print(b.get("d")) #None  如果字典中没有这个“键”后面也没有值就显示None
    print(b.get("e","f"))  # f  如果字典中没有这个“键”就显示后面的“值”
    print(b.get("a","hh"))  # b  如果字典中有这个“键”,就显示字典的“值”
  • 相关阅读:
    ExtJS 基础解析之【Ext.Window】
    螺旋队列算法分析 (转载)
    字符串NSString中去掉空格
    iOSCocos2d使用Zwoptex生成plist文件
    获取网上流视频总时长和当前播放时长
    popToViewController导航条跳转的用法
    iOS: Device token and registerForRemoteNotificationTypes, didReceiveRemoteNotification
    UILabel的详细使用及特殊效果
    UITextView使用sizeWithFont:计算自适应文本高度
    iPhone开放播放gif动画
  • 原文地址:https://www.cnblogs.com/TKOPython/p/12313709.html
Copyright © 2011-2022 走看看