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 如果字典中有这个“键”,就显示字典的“值”