python 序列化及其相关模块

Python序列化及其相关模块详解

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序列化

python 序列化及其相关模块

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

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

Like (0)
Donate 微信扫一扫 微信扫一扫
K-seo的头像K-seoSEO优化员
Previous 2023-11-08 17:56
Next 2023-11-08 17:57

相关推荐

  • debian7安装图形化界面

    在Debian 7上安装Python 3.5.1Python是一种广泛使用的高级编程语言,其设计哲学强调代码的可读性和简洁的语法,Python支持多种编程范式,包括面向对象、命令式、函数式和过程式编程,Python的设计哲学强调代码的可读性和简洁的语法(尤其是使用空格缩进划分代码块,而非使用大括号或关键字)。Python 3.5.1是……

    2024-01-24
    0161
  • python 列表推导

    Python列表推导是一种简洁、高效的创建列表的方法,通过一个表达式和一个循环语句,可以快速生成一个新的列表。

    2024-01-04
    0177
  • python创建线程的方法

    Python创建线程的方法有很多,其中最常用的是使用threading模块。threading模块提供了一个简单的方法来创建和管理线程,下面我们详细介绍如何使用threading模块创建线程。1. 导入threading模块在开始创建线程之前,我们需要先导入threading模块,通常情况下,我们只需要导入threading模块即可。……

    2024-01-11
    0196
  • python导入turtle库的方法

    您可以使用以下代码导入turtle库:import turtle。

    2024-01-04
    0391
  • python的基本存储方式有哪些

    Python的基本存储方式有:变量、列表、元组、字典、集合。

    2024-05-22
    0113
  • 大学生免费云服务器

    什么是大学生免费云服务器?大学生免费云服务器,顾名思义,是为大学生提供的免费云存储和计算服务,这种服务通常由一些知名的云服务提供商提供,如阿里云、腾讯云等,通过使用这些免费的云服务器,大学生可以在不花费太多金钱的情况下,获得一个稳定、安全、高效的云存储和计算环境,从而满足他们的学习和研究需求。如何获取大学生免费云服务器?1、注册账号:……

    2024-01-28
    0193

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

免备案 高防CDN 无视CC/DDOS攻击 限时秒杀,10元即可体验  (专业解决各类攻击)>>点击进入