JSON序列化
作者:互联网
import json
#json转换为字符串的特别是字符串两边是双引号,无论愿数据字符串是单引号还是双引号都会转换为
#双引号。包括字典里的key还是value都是一样变为双引号,只要反序列化的数据里的数据是双引号都可以用json。loads
#获取不必非的用dumps转换过的
# cit={"A":1,"B":2,}
# cit=json.dumps(cit)
# with open("file","w") as f: #利用JSON.dumps序列化对象
# f.write(cit)
# with open("file") as f:
# data= json.loads(f.read()) #利用json.loads反序列化对象
# print(data,type(data))
# with open("file1","w") as f:
# json.dump(cit,f) #json.dump只用于文件存取 代替了json.dumps(字典)+f.write(字典)
# with open("file1") as f:
# data=json.load(f) #等同于data= json.loads(f.read())
# print(data,type(data))
import pickle #类似于json,转换的为字节 # dic={"aa":1,"bb":2} # data=pickle.dumps(dic) # with open("file2","wb") as f: # f.write(data) # with open("file2","rb") as f: #方法和json基本上类似 # data=pickle.loads(f.read()) # print(data,type(data))
import shelve #将一个字典放入文本 f={}
import pickle #类似于json,转换的为字节 # dic={"aa":1,"bb":2} # data=pickle.dumps(dic) # with open("file2","wb") as f: # f.write(data) # with open("file2","rb") as f: #方法和json基本上类似 # data=pickle.loads(f.read()) # print(data,type(data))
import shelve #将一个字典放入文本 f={}
标签:data,json,dumps,JSON,cit,loads,序列化,open 来源: https://www.cnblogs.com/Centwei/p/15731080.html