Python的序列化与反序列化

一、什么是序列化和反序列化

The process of converting a structured object into a sequence of bytes which can be stored in a file system or database or sent through the network is called serialization. The reversed process is called deserialization, which means reconverting a sequence of bytes into a structured object. Creating an object via deserialization is usually faster than initializing from a class.

将结构化对象转换为可存储在文件系统或数据库中或通过网络发送的字节序列的过程称为串行化。相反,将字节序列转换为结构化对象的过程称为反序列化。

[En]

The process of converting a structured object into a sequence of bytes that can be stored in a file system or database or sent over a network is called serialization. Conversely, the process of converting a byte sequence into a structured object is called deserialization.

Python的序列化与反序列化

理解:
比如你有一个Python程序收集了网上很多的数据,然后经过转换成字节序列存储到了数据库,我的另一个Java程序去这个数据库读取那些数据做分析。Python把数据转换成字节序列存储在数据库的过程叫序列化,Java读取数据库的字节序列再格式化成Java可读的形式叫反序列化。

二、Python实现序列化的方式

基于文本的序列化格式:CSV, JSON, XML, YAML, TOML
基于二进制的序列化格式:protobuf, Avro, pickle

三、pickle模块实现序列化和反序列化

1)序列化

import pickle
data = {
'a': [1, 2.0, 3, 4+6j],
'b': ("character string", b"byte string"),
'c': {None, True, False}
}
with open("./data.picke","wb") as f:
pickle.dump(data, f, pickle.HIGHEST_PROTOCOL)

2)反序列化

import pickle
with open('data.pickle', 'rb') as f:
# The protocol version used is detected automatically, so we do not
# have to specify it.

data = pickle.load(f)
print(data)

四、json模块的序列化和反序列化

1)序列化

import json
data = {
"name": "Satyam kumar",
"place": "patna",
"skills": [
"Raspberry pi",
"Machine Learning",
"Web Development"
],
"email": "xyz@gmail.com",
"projects": [
"Python Data Mining",
"Python Data Science"
]
}
with open("data_file.json", "w") as write:
json.dump(data, write)

2)反序列化

import json
with open("data_file.json", "r") as read_content:
print(json.load(read_content))

五、pickle模块与json模块的对比

1)JSON是一个文本序列化格式,而pickle是一个二进制序列化格式;
2)JSON是可以直接阅读的,而pickle不是;
3)JSON是可互操作的,在Python系统之外广泛使用,而pickle则是Python专用的;
4)默认情况下,JSON 只能表示 Python 内置类型的子集,不能表示自定义的类;但 pickle 可以表示大量的 Python 数据类型。

写在最后

李先生(Lemon),高级运维工程师(自称),SRE专家(目标),梦想在35岁买一辆保时捷。喜欢钻研底层技术,认为底层基础才是王道。一切新技术都离不开操作系统(CPU、内存、磁盘)、网络等。坚持输入输出,记录自己学习的点滴,在平凡中坚持前行,总有一天会遇见不一样的自己。公众号:运维汪(ID:Leeeee_Li)。

Original: https://www.cnblogs.com/lemon-le/p/14812645.html
Author: Captain_Li
Title: Python的序列化与反序列化

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/10700/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

免费咨询
免费咨询
扫码关注
扫码关注
联系站长

站长Johngo!

大数据和算法重度研究者!

持续产出大数据、算法、LeetCode干货,以及业界好资源!

2022012703491714

微信来撩,免费咨询:xiaozhu_tec

分享本页
返回顶部