• 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  如果字典中有这个“键”,就显示字典的“值”
  • 相关阅读:
    数论笔记
    哈哈哈
    闭关修炼屯题中,期末考完A
    acm几何
    POJ
    Educational Codeforces Round 42 (Rated for Div. 2) D.Merge Equals (优先队列)
    Educational Codeforces Round 42 (Rated for Div. 2) C. Make a Square (dfs)
    牛客网 VVQ 与线段 (优先队列或线段树或RMQ)
    Educational Codeforces Round 41 (Rated for Div. 2) C.Chessboard (DP)
    Educational Codeforces Round 41 (Rated for Div. 2)D. Pair Of Lines
  • 原文地址:https://www.cnblogs.com/TKOPython/p/12313709.html
Copyright © 2020-2023  润新知