序列化模块 jason,pickel

模块:别人写好的功能放在一个文件里,所以python十分强大,因为开源,所以别人写好上传的模块拿过来就可以用了,不用自己写
内置模块:安装python解释器对的时候一起装上的,
第三方模块,扩展模块:需要自己安装
自定义模块:你自己写的py文件

序列化模块 :把数据类型转换成str,bytes类型。哪里用它,数据的持久化存储:文件存储,网络传输

什么叫序列化
把一个数据类型转换成字符串,bytes类型的过程就是序列化
为什么要把一个数据类型序列化
当你需要把一个数据类型储存在文件中
当你需要把一个数据类型通过网络传输的时候

python源码对pickle模块的描述

picklle 模块
The pickle module implements binary protocols for serializing and de-serializing a Python object structure.pickle
模块通过序列化和反序列化一个python对象结构实现了二进制协议 。
“Pickling” is the process whereby a Python object hierarchy is converted into a byte stream, and “unpickling” is the inverse operation, whereby a byte stream (from a binary file or bytes-like object) is converted back into an object hierarchy. Pickling (and unpickling) is alternatively known as “serialization”, “marshalling,” [1] or “flattening”;
“pickling”是将Python对象层次结构转换为字节流的过程,而“unpickling”是反向操作,由此将字节流(来自二进制文件或类似于字节的对象)转换回对象层次结构。pickling(和unpickling)可替换地称为“序列化”、“编组”、“1”或“扁平化”;
Comparison with json 对比json
- JSON is a text serialization format (it outputs unicode text, although most of the time it is then encoded to utf-8), while pickle is a binary serialization format;
JSON是一种文本序列化格式(它输出unicode文本,尽管大多数时候它被编码为utf-8),而pickle是一种二进制序列化格式;
- JSON is human-readable, while pickle is not;
JSON是人类可读的,而泡菜不是。
- JSON is interoperable and widely used outside of the Python ecosystem, while pickle is Python-specific;
JSON是可操作的,在Python生态系统之外被广泛使用,而PACKLE是Python特定的;
- JSON, by default, can only represent a subset of the Python built-in types, and no custom classes; pickle can represent an extremely large number of Python types (many of them automatically, by clever usage of Python’s introspection facilities; complex cases can be tackled by implementing specific object APIs).
缺省情况下,JSON只能表示Python内置类型的子集,而不能表示自定义类;pickle可以表示非常大量的Python类型(其中许多类型通过巧妙地使用Python的内省功能自动生成;可以通过实现特定功能来处理复杂情况对象API)。


import json
stu = {'name': 'laura', 'sex': 'female'}
ret = json.dumps(stu)  # 序列化过程
print(stu, type(stu))  # {'name': 'laura', 'sex': 'female'} <class 'dict'>
print(ret, type(ret))  # {"name": "laura", "sex": "female"} <class 'str'>

d = json.loads(ret)  # 反序列化过程
print('d', type(d))  # d <class 'dict'>

lst = [1, 2, 3, 4, 'aaa', 'bbb']
ret = json.dumps(lst)  # 序列化过程

json的优点
  # 所有语言都通用

缺点
 # 只支持非常少的数据类型
 对数据类型对的额约束很苛刻
 只支持: 数字 字符串 列表 字典  (如果序列化元组,会把元组转换成列表,因为两者很像。

import pickle
stu = {'name': 'laura', 'sex': 'female', 1: ('a', 'b')}
ret = pickle.dumps(stu)  # pickle 可以序列化元组和数字,相对于json,适用的范围更广
print(ret)  # 转换成了bytes类型 # b'\x80\x03}q\x00(X\x04\x00\x00\x00nameq\x01X\x05\x00\x00\x00lauraq\x02X\x03\x00\x00\x00sexq\x03X\x06\x00\x00\x00femaleq\x04K\x01X\x01\x00\x00\x00aq\x05X\x01\x00\x00\x00bq\x06\x86q\x07u.'
d = pickle.loads(ret)
print(d, type(d))  # 不会改变数据类型#  {'name': 'laura', 'sex': 'female', 1: ('a', 'b')} <class 'dict'>


import pickle  # (只支持python语言,支持几乎所有的数据类型)
class Course():
    def __init__(self, name, price):
        self.name = name
        self.price = price

python = Course('python', 19899)
ret = pickle.dumps(python)  # 序列化对象
print(ret)  # 转换成了bytes #  b'\x80\x03c__main__\nCourse\nq\x00)\x81q\x01}q\x02(X\x04\x00\x00\x00nameq\x03X\x06\x00\x00\x00pythonq\x04X\x05\x00\x00\x00priceq\x05M\xbbMub.'

p = pickle.loads(ret)  # 反序列化对象
print(p.name, p.price)   # python 19899  



pickle 在文件中储存对象 

class Course:
    def __init__(self, name, price):
        self.name = name
        self.price = price

import pickle 

python = Course('python', 1200)
linux = Course('linux', 3222)
bigdata = Course('bigdata', 4533) 

def my_dump(course):
    with open('courseinfo', 'ab') as f:  # pickle必须以+b的格式打开,因为pickle写入的是二进制格式
        pickle.dump(course, f) 

my_dump(python) 

my_dump(linux) 

my_dump(bigdata)   # 把三个类的实例化对象存在文件中

with open('courseinfo', 'rb') as f:
    while True:
        try:
            content = pickle.load(f)
            print(content.name)  # 打印对象的属性,如果找不到该类,同样找不到该对象的属性,这是python的一个弊端。
        except EOFError:
            break
>>>
python
linux
bigdata

猜你喜欢

转载自blog.csdn.net/weixin_42233629/article/details/82355642
今日推荐