前言
Python
的json
和pickle
模块实现了数据的序列和反序列化。在使用和功能实现上没有太大区别,那么接下来我们分别来看看这两个模块之间的使用和对比。
① json模块
一、说到json
这个模块我相信大家使用最多的功能就是它对json
字符串的序列化和反序列化,分别对应json.dumps()
和json.loads()
方法:
import json
data = {
'name' : 'Trump',
'flag' : 'blonde',
'hobby' : 'build the wall'
}
json_str = json.dumps(data)
print(json_str)
print(type(json_str))
# 将Python数据结构转换成json字符串
# 结果为:{"name": "Trump", "flag": "blonde", "hobby": "build the wall"}
# <class 'str'>
下面演示如何将一个json
编码的字符串转换回一个Python
数据结构:
data = json.loads(json_str)
print(data)
print(type(data))
# 结果为:{'name': 'Trump', 'flag': 'blonde', 'hobby': 'build the wall'}
# <class 'dict'>
二、那么还有另外一种应用场景,就是我们需要将数据以json
字符串的形式写入到文件,或者将文件中的json
字符串读取出来并转换成Python
数据结构,分别对应的是json.dump()
和json.load()
方法:
with open('data.json', 'w') as f:
json.dump(data, f)
with open('data.json', 'r') as f:
data = f.readline()
print(data)
print(type(data))
# 结果为:{"name": "Trump", "flag": "blonde", "hobby": "build the wall"}
# <class 'str'>
把文件中的json
字符串读取出来并转化成Python
数据结构:
with open('data.json', 'r') as f:
data = json.load(f)
print(data)
print(type(data))
# 结果为:{'name': 'Trump', 'flag': 'blonde', 'hobby': 'build the wall'}
# <class 'dict'>
其实json
模块可用于多平台和多语言的字符串与Python
数据类型进行互换,但是它只能序列化Python
最基本的数据类型(str、tuple、list、dict、set)!
② pickle模块
pickle
模块与json
模块的功能类似,但是,pickle
模块只能对Python
的数据结构序列化!相较于json
模块只能序列化便宜他还能最基本的数据类型,pickle能对Python中的所有数据类型进行序列化,包括类和函数!!!
import pickle
data = {
'name' : 'Trump',
'flag' : 'blonde',
'hobby' : 'build the wall'
}
pickle_bytes = pickle.dumps(data)
print(pickle_bytes)
print(type(pickle_bytes))
# 结果为:b'\x80\x03}q\x00(X\x04\x00\x00\x00nameq\x01X\x05\x00\x00\x00Trumpq\x02X\x04\x00\x00\x00flagq\x03X\x06\x00\x00\x00blondeq\x04X\x05\x00\x00\x00hobbyq\x05X\x0e\x00\x00\x00build the wallq\x06u.'
# <class 'bytes'>
# 注意:这里pickle序列化后的数据类型为bytes二进制数据
反序列化:
data = pickle.loads(json_str)
print(data)
print(type(data))
# 结果为:{'name': 'Trump', 'flag': 'blonde', 'hobby': 'build the wall'}
# <class 'dict'>
写入到文件与加载文件中的二进制数据的方法分别为pickle.dump()和pickle.load():
# 写入到文件
with open('bytes.json', 'wb') as f:
pickle.dump(data, f)
# 查看文件中数据
with open('bytes.json', 'rb') as f:
data = f.readline()
print(data)
print(type(data))
# 结果为:b'\x80\x03}q\x00(X\x04\x00\x00\x00nameq\x01X\x05\x00\x00\x00Trumpq\x02X\x04\x00\x00\x00flagq\x03X\x06\x00\x00\x00blondeq\x04X\x05\x00\x00\x00hobbyq\x05X\x0e\x00\x00\x00build the wallq\x06u.'
# <class 'bytes'>
# 从文件中读取二进制数据
with open('bytes.json', 'rb') as f:
data = pickle.load(f)
print(data)
print(type(data))
# 结果为:{'name': 'Trump', 'flag': 'blonde', 'hobby': 'build the wall'}
# <class 'dict'>
③ json模块和pickle模块的性能对比
做个小测试:
from time import perf_counter as pc
# 构造数据
Data = [data for i in range(5000)]
t1 = pc()
with open('jsondump.json', 'w') as f:
json.dump(Data, f)
print(pc()-t1)
# jsondump.json文件大小为:320KB
t2 = pc()
with open('jsondump_indent.json', 'w') as f:
json.dump(Data, f, indent=4)
print(pc()-t2)
# jsondump_indent.json文件大小为:515KB
t3 = pc()
with open('pickledump.json', 'wb') as f:
pickle.dump(Data, f)
print(pc()-t3)
# pickle_indent.json文件大小为:10.1KB
# 时间结果分别为:
# 0.036876599999999995
# 0.045622
# 0.0008390999999999954
一、在存储空间上,json.dump()
存储带有缩进格式的数据比不带缩进格式的数据占用空间更大;相较于json
,pickle.dump()
存储相同的数据占用空间更小。
二、在存储效率上,使用pickle
比使用json
要快大约44
倍(在我的Windows
上是这样的结果),这个可能与存储的数据类型和数据量大小还有使用的平台有一定的关联,此测试结果不具有代表性,仅供大家参考!!!