首页 > 编程语言 > 详细

python序列化模块

时间:2018-08-09 22:54:38      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:如果   type   操作   结构   结果   utf-8   反序列化   pic   对象的序列化   

 #什么叫序列化模块:
#     # 将原本的字典,列表等内容转换成一个字符串的过程就叫做序列化
# #序列化的目的:
#     #1.以某种存储形式使自定义对象持久化
#     #2.将对象从一个地方传递到另外一个地方
#     #3.使程序更具维护性.
#       #  str>>>> 反序列化>>>>数据结构
#       #  数据结构>>>>序列化>>>>>>>str
#
#     # json
#     # json模块提供了四个功能:dumps dump loads load
# import json
# #  dumps 和 loads
# dic = {"k1":"v1","k2":"v2","k3":"v3"}
# str_dic = json.dumps(dic)    #  序列化:将一个字典转换成一个字符串
# print(type(str_dic),str_dic)   #  <class ‘str‘> {"k1": "v1", "k2": "v2", "k3": "v3"}
# # 注意: json转换完的字符串类型的字典中的字符串是由""(双标点)表示
#
# dic2 = json.loads(str_dic)  #  反序列化:将一个字符串格式的字典转换成一个字典
# print(type(dic2),dic2)    # <class ‘dict‘> {‘k1‘: ‘v1‘, ‘k2‘: ‘v2‘, ‘k3‘: ‘v3‘}
# #注意: 用json的loads功能处理的字符串类型的字典中的字符串必须由""表示
#
# list_dic = [1,[‘a‘,‘b‘,‘c‘],3,{‘k1‘:‘v1‘,‘k2‘:‘v2‘}]
# str_dic = json.dumps(list_dic)   #  可以处理嵌套的数据类型
# print(type(str_dic),str_dic)    #  <class ‘str‘> [1, ["a", "b", "c"], 3, {"k1": "v1", "k2": "v2"}]
# list_dic2 = json.loads(str_dic)
# print(type(list_dic2),list_dic2)  # <class ‘list‘> [1, [‘a‘, ‘b‘, ‘c‘], 3, {‘k1‘: ‘v1‘, ‘k2‘: ‘v2‘}]
#
#
# # dump 和load
#
# f = open(‘json_file‘,‘w‘)
# dic4 = {‘k1‘:‘v1‘,‘k2‘:‘v2‘,‘k3‘:‘v3‘}
# json.dump(dic4,f)  #  dump方法接收一个文件句柄,直接将字典转换成hson字符串后写入文件
# f.close()   #  关闭文件
#
# f = open(‘json_file‘)
# dic5 = json.load(f)  # load方法接收一个文件句柄,直接将文件中的字符串转换成数据结构返回
# f.close()
# print(type(dic5),dic5)     #  <class ‘list‘> [1, [‘a‘, ‘b‘, ‘c‘], 3, {‘k1‘: ‘v1‘, ‘k2‘: ‘v2‘}]
#import json
#  dumps和dump中ensure_ascii关键字
# f = open(‘file‘,‘w‘,encoding="utf-8")
# json.dump({‘国籍‘:‘中国‘},f)
# ret = json.dumps({‘国际‘:‘中国‘})
# f.write(ret+"\n")   #  写入之后里面是字节码
# f.close()
#
# json.dump({‘国籍‘:‘中国‘},f,ensure_ascii=False)
# ret1 = json.dumps({‘国籍‘:‘中国‘},ensure_ascii=False)
# f.write(ret1+"\n")    #  写入之后显示中文
# f.close()

#json的格式化输出
# import json
# data = {‘username‘:[‘李华‘,‘二愣子‘],‘sex‘:‘male‘,‘age‘:16}
# json_dic2 = json.dumps(data,sort_keys=True,indent=2,separators=(‘,‘,‘:‘),ensure_ascii=False)
# print(json_dic2)    # 为了用户方便看,存入文件浪费内存
# 结果:
#{
#   "age":16,
#   "sex":"male",
#   "username":[
#     "李华",
#     "二愣子"
#   ]
# }


#json用法写的一些注意事项

#import json
#json格式的限制1,json格式的key必须是字符串数据类型
#json格式中的字符串只能是""(双引号)

#如果是数字为key,那么dump之后会强行转成字符串数据类型
# dic = {1:2,3:4}
# str_dic = json.dumps(dic)
# print(type(str_dic),str_dic)  # <class ‘str‘> {"1": 2, "3": 4}
# new_dic = json.loads(str_dic)
# print(type(new_dic),new_dic)  #  <class ‘dict‘> {‘1‘: 2, ‘3‘: 4}


# json是否支持元组,对元组做value的字典会把元组强制转换成列表
# dic = {‘abc‘:(1,2,3)}
# str_dic = json.dumps(dic)
# print(type(str_dic),str_dic) #  <class ‘str‘> {"abc": [1, 2, 3]}  转成列表
# new_dic = json.loads(str_dic)
# print(new_dic)  #   {‘abc‘: [1, 2, 3]}  转换回去value还是列表

#json是否支持元组做key,不支持
# dic = {(1,2,3):‘abc‘}
# str_dic = json.dumps(dic)   #  报错   键必须是str、int、float、bool或None,而不是tuple

# 对列表的dump
# lst = ["aaa",123,‘bbb‘,4.66]
# with open(‘json_demo‘,‘w‘) as f:
#     json.dump(lst,f)
# with open(‘json_demo‘) as f:
#     ret = json.load(f)
#     print(ret)  # [‘aaa‘, 123, ‘bbb‘, 4.66]
#

#能不能多次dump数据到文件里,可以多次dump到文件里但是不能load出来
# dic = {‘abc‘:(1,2,3)}
# lst = ["avc",123,44,34]
# with open(‘json_demo‘,‘w‘) as f:
#     json.dump(lst,f)
#     json.dump(dic,f)  #  多次dump到文件
# with open(‘json_demo‘) as f:
#     ret = json.load(f)   # 报错
#     print(ret)
#


#想dump多个数据进入文件,用dumps
# dic = {‘abc‘:(1,2,3)}
# lst = [‘abc‘,123]
# with open(‘json_demo‘,‘w‘) as f:
#     str_lst = json.dumps(lst)
#     str_dic = json.dumps(dic)
#     f.write(str_lst+"\n")
#     f.write(str_dic+"\n")
#
# with open(‘json_demo‘) as f:
#     for line in f:
#         ret = json.loads(line)
#         print(ret)   #  全部取出
# 结果:
        #[‘abc‘, 123]
        #{‘abc‘: [1, 2, 3]}

# 中文格式的 ensure_ascii = False
# dic = {‘ac‘:(1,23,3),"国家":"中国"}
# ret = json.dumps(dic,ensure_ascii=False)
# print(ret)   # {"ac": [1, 23, 3], "国家": "中国"}
#
# dic_new = json.loads(ret)
# print(dic_new)  #  {‘ac‘: [1, 23, 3], ‘国家‘: ‘中国‘}

# 集合set不能被dump和dumps


# pickle模块的四个功能:dumps,dump(序列化,存),loads(反序列化,读),load
#  pickle可以将python中任意的数据类型序列化

#dump的结果是bytes,dump用的f文件句柄需要以‘wb‘的形式打开,load所用的f是‘rb‘模式读取
#几乎支持所有数据类的序列化
#对于对象的序列化需要这个对象对象的类在内存中
#对于多次dump\load的操作做了良好的处理
#import pickle
# dic = {1:(1,2,3),(‘a‘,‘b‘):4}
# pic_dic = pickle.dumps(dic)
# print(type(pic_dic),pic_dic)   #  <class ‘bytes‘> b‘\x80\x03}q\x00(K\x01K\  bytse类型
#
# new_dic = pickle.loads(pic_dic)
# print(new_dic)    # {1: (1, 2, 3), (‘a‘, ‘b‘): 4}

#pickle支持几乎所有对象的
# class Student:
#     def __init__(self,name,age):
#         self.name = name
#         self.age = age
# alex = Student("alex",40)
# ret = pickle.dumps(alex)
# print(ret)   # 字节
#
# pic_ret = pickle.loads(ret)
# print(pic_ret)   # <__main__.Student object at 0x0000000002696EB8>
# print(pic_ret.name)    #alex
# print(pic_ret.age)     #  40

import pickle
# class Student:
#     def __init__(self,name,age):
#         self.name = name
#         self.age = age
# alex = Student("alex",80)
# with open(‘pickle_demo‘,‘wb‘) as f:
#     pickle.dump(alex,f)
# with open(‘pickle_demo‘,"rb") as f:
#     ret = pickle.load(f)
#     print(ret)  #  <__main__.Student object at 0x00000000028A6358>
#     print(ret.name)
#
# with open(‘pickle_demo‘,‘wb‘) as f:
#     pickle.dump({‘k1‘:‘v1‘},f)
#     pickle.dump({‘k2‘:‘v2‘},f)
#
# with open(‘pickle_demo‘,‘rb‘) as f:   #  不能用for循环,不知道有多少文件
#     while 1:
#         try:
#             print(pickle.load(f))
#         except EOFError:  # 异常捕获
#             break

#结果
#  {‘k1‘: ‘v1‘}
#  {‘k2‘: ‘v2‘}

#shelve 序列化工具只有一个open方法
import shelve
f = shelve.open(‘shelve_demo‘)
f[‘key‘] = {‘k1‘:(1,2,3),‘k2‘:‘v2‘}
f.close()


f = shelve.open(‘shelve_demo‘)
content = f[‘key‘]
f.close()
print(content)    #  {‘k1‘: (1, 2, 3), ‘k2‘: ‘v2‘}

#shelve  如果写定的一个文件,改动的比较少,读取比较多,且大部分读取都要
#基于某个key获得某个value

  

python序列化模块

标签:如果   type   操作   结构   结果   utf-8   反序列化   pic   对象的序列化   

原文:https://www.cnblogs.com/caodneg7/p/9451975.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 bubuko.com 版权所有 鲁ICP备09046678号-4
打开技术之扣,分享程序人生!
             

鲁公网安备 37021202000002号