Python序列化及其相关模块详解
在计算机编程中,序列化是一种将对象转换为字节流的过程,以便将其存储在磁盘上或通过网络传输,反序列化则是将字节流转换回原始对象的过程,Python提供了多种序列化和反序列化的方法,本文将详细介绍这些方法及其相关模块。
二、Python内置的序列化方法
1. 字符串序列化
Python中的字符串可以直接用于序列化和反序列化,可以使用`pickle`模块将一个对象序列化为字符串,然后再将其反序列化为原始对象。
import pickle # 创建一个对象 data = {'name': '张三', 'age': 30} # 将对象序列化为字符串 serialized_data = pickle.dumps(data) print("序列化后的数据:", serialized_data) # 将字符串反序列化为对象 deserialized_data = pickle.loads(serialized_data) print("反序列化后的数据:", deserialized_data)
2. JSON序列化
JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,易于阅读和编写,Python中的`json`模块提供了将对象转换为JSON字符串以及将JSON字符串转换为对象的函数。
import json # 创建一个对象 data = {'name': '张三', 'age': 30} # 将对象序列化为JSON字符串 serialized_data = json.dumps(data) print("序列化后的数据:", serialized_data) # 将JSON字符串反序列化为对象 deserialized_data = json.loads(serialized_data) print("反序列化后的数据:", deserialized_data)
三、第三方库的序列化方法
1. MessagePack
MessagePack是一种高效的二进制序列化格式,与JSON相比,它具有更小的体积和更快的解析速度,Python中的`msgpack`库提供了将对象转换为MessagePack字节流以及将MessagePack字节流转换为对象的函数。
```python
import msgpack
import msgpack_numpy as mnp
from collections import namedtuple, defaultdict, OrderedDict, Counter, ChainMap, deque, Sequence, Mapping, Set, MutableMapping, MutableSequence, UserDict, UserList, Tuple, FrozenSet, Iterable, Iterator, GeneratorType, Reversed, List, Dict, Set, ImmutableSet, Union, Hashable, MutableSet, Callable, TypeVar, Optional, cast, Any, overload, Literal, Sized, getattro, reduce, partialmethod, wraps, finalize, total_ordering, heapq, bisect, UserString, TextWrapper, ArrayType, UnicodeType, binary_type, bool_type, bytearray_type, bytes_type, datetime_type, float_type, int_type, list_type, long_type, object_hook, object_pairs_hook, reprlib, string_types, text_type, hexstr, basestring, filterfalse, iteritems, ziplongest, map as mapping_map, reversed as mapping_reversed, set as mapping_set, frozenset as mapping_frozenset, OrderedDict as mapping_OrderedDict, dict as mapping_dict, defaultdict as mapping_defaultdict, Counter as mapping_Counter, ChainMap as mapping_ChainMap, deque as mapping_deque, Sequence as mapping_Sequence, Mapping as mapping_Mapping, Set as mapping_Set, MutableMapping as mapping_MutableMapping, MutableSequence as mapping_MutableSequence, UserDict as mapping_UserDict, UserList as mapping_UserList, Tuple as mapping_Tuple, FrozenSet as mapping_FrozenSet, Iterable as mapping_Iterable, Iterator as mapping_Iterator, GeneratorType as mapping_GeneratorType, Reversed as mapping_Reversed; from collections.abc import Collection; from collections.abc import MutableMapping; from collections.abc import MutableSequence; from collections.abc import Set; from collections.abc import Iterable; from collections.abc import Iterator; from collections.abc import Container; from collections.abc import Sized; from collections.abc import Callable; from collections.abc import TypeVar; from collections.abc import Optional; from collections.abc import cast; from collections.abc import Any; from collections.abc import overload; from collections.abc import Literal; from collections.abc import total_ordering; from collections.abc import Heapq; from collections.abc import Bisect; from collections.abc import UserString; from collections.abc import TextWrapper; from collections.abc import ArrayType; from collections.abc import UnicodeType; from collections.abc import binary_type; from collections.abc import bool_type; from collections.abc import bytearray_type; from collections.abc import bytes_type; from collections.abc import datetime_type; from collections.abc import float_type; from collections.abc import int_type; from collections.abc import list_type; from collections.abc import long_type; from collections.abc import object_hook; from collections.abc import object_pairs_hook; from collections.abc import reprlib; from collections.abc import string_types; from collections.abc import text_type; from collections.abc import hexstr; from collections.abc import basestring; from collections.abc import filterfalse; from collections.abc import iteritems; from collections.abc import ziplongest; from msgpack import packb as msgpack_packb; from msgpack import unpackb as msgpack_unpackb; from msgpack.ext import register_extensions; register_extensions() # 注册扩展包; # 创建一个对象; data = {'name': '张三', 'age': 30} # 将对象转换为MessagePack字节流; serialized_data = msgpack.packb(data) print("序列化后的数据:", serialized_data) # 将MessagePack字节流转换为对象; deserialized_data = msgpack.unpackb(serialized_data) print("反序列化后的数据:", deserialized_data) # 使用msgpack库处理NumPy数组; arr = np.array([1, 2]) packed = msgpack.packb(arr) unpacked = msgpack.unpackb(packed) print("NumPy数组序列化后的数据:", packed) print("NumPy数组反序列化后的数据:", unpacked) print("NumPy数组反序列化后的数组类型:", type(unpacked)) # 使用msgpack库处理嵌套的对象; nested = {'a': {'b': {'c': [1]}}} packed = msgpack.packb(nested) unpacked = msgpack.unpackb(packed) print("嵌套对象序列化后的数据:", packed) print("嵌套对象反序列化后的数据:", unpacked) print("嵌套对象反序列化后的对象类型:", type(unpacked)) # 使用msgpack库处理其他类型的数据; data = (1234567890123456789012345678901234567890L) packed = msgpack.packb(data) unpacked = msgpack.unpackb(packed) print("长整型数据序列化后的数据:", packed) print("长整型数据反序列化后的数据:", unpickled) print("长整型数据反序列化后的数据类型:", type(unpickled)) # 使用msgpack库处理自定义类; class MyClass: def __init__(self): self.myfield = "Hello" packed = msgpack.packb(MyClass()) unpickled = msgpack->unpickleb(packed) print("自定义类实例序列化后的数据:", packed) print("自定义类实例反序列化后的数据:", unpickled) print("自定义类实例反序列化后的对象类型:", type(unpickled)) # 使用msgpack库处理其他类型的数据; data = b'x01\x02x03\x04x05\x06x07\x08' packed = msgpack.packb(data) unpickled = msgpack->unpickleb(packped) print("字节串数据序列化后的数据:", packed) print("字节串数据反序列化后的数据:", unpickled) print("字节串数据反序列化后的数据类型:", type(unpickled)) # 使用msgpack库处理其他类型的数据; data = True packed = msgpack->
原创文章,作者:K-seo,如若转载,请注明出处:https://www.kdun.cn/ask/9965.html