com的域名是指商业网站的域名.,社区建站网站系统,做推广的都是怎么推,怎么做百度快照让网站排前面Python Pickle 与 JSON 序列化详解#xff1a;存储、反序列化与对比 文章目录 Python Pickle 与 JSON 序列化详解#xff1a;存储、反序列化与对比一 功能总览二 Pickle1 应用2 序列化3 反序列化4 系统资源对象1#xff09;不能被序列化的系统资源对象2#xff09;强行序列…Python Pickle 与 JSON 序列化详解存储、反序列化与对比 文章目录 Python Pickle 与 JSON 序列化详解存储、反序列化与对比一 功能总览二 Pickle1 应用2 序列化3 反序列化4 系统资源对象1不能被序列化的系统资源对象2强行序列化系统资源对象 三 Json1 应用2 序列化3 反序列化4 不能序列化 class 类 四 Pickle 和 Json 对比五 完整代码示例六 源码地址 在 Python 中序列化是保存数据和对象的重要方式其中 Pickle 和 JSON 是常用的两种序列化方法。本文详细介绍了如何使用 Pickle 和 JSON 进行数据和类的序列化与反序列化操作。通过 Pickle能够将 Python 对象保存为二进制文件而 JSON 更适用于跨语言的数据交换。文章包含了 Pickle 的序列化、反序列化以及如何处理系统资源对象。还涵盖了 JSON 的基本操作及其与 Pickle 的对比帮助开发者根据不同场景选择合适的序列化方式。
一 功能总览
PickleJsonpickle.dumps()json.dumps()pickle.dump()json.dump()pickle.load()json.load()
二 Pickle
1 应用
使用 pickle 去 dumps 数据 # import pickle data {filename: f1.txt, create_time: today, size: 111}print(pickle.dumps(data))2 序列化
将数据转换成文件储存 # 用 pickle.dump() 将字典直接转换成一个文件。data {filename: f1.txt, create_time: today, size: 111}with open(data.pkl, wb) as f:pickle.dump(data, f)print(os.listdir())with open(data.pkl, rb) as f:data pickle.load(f)print(data)3 反序列化
pickle 可以将 类打包成文件然后 unpickle 还原回来File 类
class File:def __init__(self, name, create_time, size):self.name nameself.create_time create_timeself.size sizedef change_name(self, new_name):self.name new_name从 data.pkl 文件中 反序列化回来 # 打包类File类data File(f2.txt, now, 222)# 存with open(data.pkl, wb) as f:pickle.dump(data, f)# 读with open(data.pkl, rb) as f:read_data pickle.load(f)print(read_data.name)print(read_data.size)4 系统资源对象
1不能被序列化的系统资源对象
注依赖外部系统状态的对象不能被序列化比如 打开文件网络连接线程进程栈帧等等。
class File02:def __init__(self, name, create_time, size):self.name nameself.create_time create_timeself.size size# 打开文件self.file open(name, w)# 依赖外部系统状态的对象不能被序列化比如 打开的文件网络连接线程进程栈帧等等。# data File02(f3.txt, now, 222)# pickle 存会报错# TypeError: cannot pickle _io.TextIOWrapper object# with open(data.pkl, wb) as f:# pickle.dump(data, f)2强行序列化系统资源对象
硬要用依赖外部系统状态的对象去 pickle 保存可以规避一下看 类 File03
class File03:def __init__(self, name, create_time, size):self.name nameself.create_time create_timeself.size sizeself.file open(name, w)def __getstate__(self):# pickle 出去需要且能被 pickle 的信息pickled {name: self.name, create_time: self.create_time, size: self.size}return pickleddef __setstate__(self, pickled_dict):# unpickle 加载回来重组 classself.__init__(pickled_dict[name], pickled_dict[create_time], pickled_dict[size])示例 # 硬要用依赖外部系统状态的对象去 pickle 保存可以规避一下# pickle.dump() 会调用 __getstate__() 获取序列化的对象。 __setstate__() 在反序列化时被调用。data File03(f3.txt, now, 222)# 存with open(data.pkl, wb) as f:pickle.dump(data, f)# 读with open(data.pkl, rb) as f:read_data pickle.load(f)print(read_data.name)print(read_data.size)print()三 Json
1 应用
# Json
# import json
data {filename: f1.txt, create_time: today, size: 111}
j json.dumps(data)
print(j)
print(type(j))2 序列化
Json 储存数据
# Json 储存数据
data {filename: f1.txt, create_time: today, size: 111}
with open(data.json, w) as f:json.dump(data, f)print(直接当纯文本读)
with open(data.json, r) as f:print(f.read())3 反序列化
print(用 json 加载了读)
with open(data.json, r) as f:new_data json.load(f)
print(字典读取, new_data[filename])4 不能序列化 class 类
class File04:def __init__(self, name, create_time, size):self.name nameself.create_time create_timeself.size sizedef change_name(self, new_name):self.name new_name
# json 不能序列化保存 class
# TypeError: Object of type File is not JSON serializable
data File04(f4.txt, now, 222)
# 存会报错
# with open(data.json, w) as f:
# json.dump(data, f)
四 Pickle 和 Json 对比
对比PickleJson存储格式Python 特定的 Bytes 格式通用 JSON text 格式可用于常用的网络通讯中数据种类类功能字典列表元组等基本和 Pickle 一样但不能存类功能保存后可读性不能直接阅读能直接阅读跨语言性只能用在 Python可以跨多语言读写处理时间长需编码数据短不需编码安全性不安全除非你信任数据源相对安全
五 完整代码示例
# This is a sample Python script.# Press ⌃R to execute it or replace it with your code.
# Press Double ⇧ to search everywhere for classes, files, tool windows, actions, and settings.
import pickle
import os
import jsonclass File04:def __init__(self, name, create_time, size):self.name nameself.create_time create_timeself.size sizedef change_name(self, new_name):self.name new_nameclass File03:def __init__(self, name, create_time, size):self.name nameself.create_time create_timeself.size sizeself.file open(name, w)def __getstate__(self):# pickle 出去需要且能被 pickle 的信息pickled {name: self.name, create_time: self.create_time, size: self.size}return pickleddef __setstate__(self, pickled_dict):# unpickle 加载回来重组 classself.__init__(pickled_dict[name], pickled_dict[create_time], pickled_dict[size])class File02:def __init__(self, name, create_time, size):self.name nameself.create_time create_timeself.size sizeself.file open(name, w)class File:def __init__(self, name, create_time, size):self.name nameself.create_time create_timeself.size sizedef change_name(self, new_name):self.name new_namedef print_hi(name):# Use a breakpoint in the code line below to debug your script.print(fHi, {name}) # Press ⌘F8 to toggle the breakpoint.# 主要涉及到的功能# Pickle# pickle.dumps()# pickle.dump()# pickle.load()# Json# json.dumps()# json.dump()# json.load()data {filename: f1.txt, create_time: today, size: 111}print(pickle.dumps(data))# 用 pickle.dump() 将字典直接转换成一个文件。data {filename: f1.txt, create_time: today, size: 111}with open(data.pkl, wb) as f:pickle.dump(data, f)print(os.listdir())with open(data.pkl, rb) as f:data pickle.load(f)print(data)# 打包类File类data File(f2.txt, now, 222)# 存with open(data.pkl, wb) as f:pickle.dump(data, f)# 读with open(data.pkl, rb) as f:read_data pickle.load(f)print(read_data.name)print(read_data.size)# 依赖外部系统状态的对象不能被序列化比如 打开的文件网络连接线程进程栈帧等等。# data File02(f3.txt, now, 222)# pickle 存会报错# TypeError: cannot pickle _io.TextIOWrapper object# with open(data.pkl, wb) as f:# pickle.dump(data, f)# 硬要用依赖外部系统状态的对象去 pickle 保存可以规避一下# pickle.dump() 会调用 __getstate__() 获取序列化的对象。 __setstate__() 在反序列化时被调用。data File03(f3.txt, now, 222)# 存with open(data.pkl, wb) as f:pickle.dump(data, f)# 读with open(data.pkl, rb) as f:read_data pickle.load(f)print(read_data.name)print(read_data.size)print()# Jsondata {filename: f1.txt, create_time: today, size: 111}j json.dumps(data)print(j)print(type(j))print()# Json 储存数据data {filename: f1.txt, create_time: today, size: 111}with open(data.json, w) as f:json.dump(data, f)print(直接当纯文本读)with open(data.json, r) as f:print(f.read())print(用 json 加载了读)with open(data.json, r) as f:new_data json.load(f)print(字典读取, new_data[filename])print()# json 不能序列化保存 class# TypeError: Object of type File is not JSON serializabledata File04(f4.txt, now, 222)# 存会报错# with open(data.json, w) as f:# json.dump(data, f)# Pickle 和 Json 的不同# 存储格式 Python 特定的 Bytes 格式 通用 JSON text 格式可用于常用的网络通讯中# 数据种类 类功能字典列表元组等 基本和 Pickle 一样但不能存类功能# 保存后可读性 不能直接阅读 能直接阅读# 跨语言性 只能用在 Python 可以跨多语言读写# 处理时间 长需编码数据 短不需编码# 安全性 不安全除非你信任数据源 相对安全# Press the green button in the gutter to run the script.
if __name__ __main__:print_hi(pickle 和 json 序列化)# See PyCharm help at https://www.jetbrains.com/help/pycharm/
复制粘贴并覆盖到你的 main.py 中运行运行结果如下。
Hi, pickle 和 json 序列化
b\x80\x04\x958\x00\x00\x00\x00\x00\x00\x00}\x94(\x8c\x08filename\x94\x8c\x06f1.txt\x94\x8c\x0bcreate_time\x94\x8c\x05today\x94\x8c\x04size\x94Kou.
[new_file4.txt, Function 函数.py, new_file5.txt, 如何控制异常 try-except.py, new_file.txt, me.py, new_file2.txt, .DS_Store, new_file3.txt, f3.txt, Module 模块.py, chinese.txt, no_file.txt, module, user, 数据种类.py, __pycache__, data.json, project, 正则表达式.py, file.py, 单元测试.py, pickle 和 json序列化.py, 变量与运算.py, data.pkl, 文件目录管理.py, 条件判断.py, Class 类.py, main.py, 读写文件.py, for 和 while 循环.py, .idea]
{filename: f1.txt, create_time: today, size: 111}
f2.txt
222
f3.txt
222{filename: f1.txt, create_time: today, size: 111}
class str直接当纯文本读
{filename: f1.txt, create_time: today, size: 111}
用 json 加载了读
字典读取 f1.txt六 源码地址
代码地址
国内看 Gitee 之 pickle 和 json序列化.py
国外看 GitHub 之 pickle 和 json序列化.py
引用 莫烦 Python