1.pickle相关的灵魂拷问,who、why??
(1). who it is?
- pickle模块只能在Python中使用,python中几乎所有的数据类型(列表,字典,集合,类等)都可以用pickle来序列化
- pickle序列化后的数据,可读性差,人一般无法识别
- 持久化存储, 可以将对象以文件的形式存放在磁盘上
- 可以将文件保存任何格式,但是一般使用pkl,pickle,p、data等格式。你自创一个扩展名,例如【.alien】也可以
(2). why is it ?
- 当你想保存对象、字典的时候,ini格式、xml格式等配置文件都无法直接实现,需要再次解析一下。但pickle就可以直接解析了
- 当你想保存二进制文件的时候,pickle也能排上用场
- Pickle 是可移植的,不同操作系统,无论是mac、windows、linux都可以读取pickle文件
2.pickle如何使用,how to use ?
(1)序列化操作对象-------保存到文件
序列化对象----------------并将结果数据流写入到文件对象中。
with open("test_pickle.pkl", "wb") as f:
pickle.dump(obj, f, protocol=0)
def dump(obj, file, protocol=None):
Pickler(file, protocol).dump(obj)
class Pickler:
def __init__(self, file, protocol=None):
"""This takes a file-like object for writing a pickle data stream.
The optional protocol argument tells the pickler to use the
given protocol; supported protocols are 0, 1, 2. The default
protocol is 0, to be backwards compatible. (Protocol 0 is the
only protocol that can be written to a file opened in text
mode and read back successfully. When using a protocol higher
than 0, make sure the file is opened in binary mode, both when
pickling and unpickling.)
Protocol 1 is more efficient than protocol 0; protocol 2 is
more efficient than protocol 1.
- 序列化模式(protocol)支持[0, 1, 2]三个选项,数值越大效率越高,也代表压缩的越厉害。
- 默认是0, 基本能看出来保存的对象和列表信息,但是如果选择1、或2,基本就是乱码了,说明压缩比很高
- 后续有截图,不同压缩比看到的效果
(2)反序列化操作-------解析pickle的文件
反序列化操作--------------------将数据从文件中解析出来的过程
with open("test_pickle.pkl", "rb") as f:
obj = pickle.load(f)
3.序列化----类(Class)时候的代码
import pickle
class Person(object):
def __init__(self, name, age, blog):
self.name = name
self.age = age
self.blog = blog
def do_serializing(file):
per = Person("alien", 18, "https://blog.csdn.net/chenmozhe22")
with open(file, "wb") as f:
pickle.dump(per, f, protocol=0)
def do_unserializing(file):
with open(file, "rb") as f:
obj = pickle.load(f)
print("obj type==========>{}".format(type(obj)))
print("obj value==========>{},{},{}".format(obj.name, obj.age, obj.blog))
if __name__ == "__main__":
file_path = r'F:\MyProjects\XXXXX\TestCase\pickle_file.obj'
do_serializing(file_path)
do_unserializing(file_path)
obj type==========><class '__main__.Person'>
obj value==========>alien,18,https://blog.csdn.net/chenmozhe22
4.序列化模式(protoco)不同值的效果
protocol=0时候压缩效果:
protocol=1时候压缩效果:
5.序列化----列表(list)时候的代码
import pickle
def do_serializing(file):
info_list = ["alien", 18, "https://blog.csdn.net/chenmozhe22"]
with open(file, "wb") as f:
pickle.dump(info_list, f, protocol=0)
def do_unserializing(file):
with open(file, "rb") as f:
obj = pickle.load(f)
print("obj type==========>{}".format(type(obj)))
print("obj value==========>{},{},{}".format(obj[0], obj[1], obj[2]))
if __name__ == "__main__":
file_path = r'F:\MyProjects\FT_AutoTestDemo\TestCase\pickle_file.pkl'
do_serializing(file_path)
do_unserializing(file_path)
obj type==========><type 'list'>
obj value==========>alien,18,https://blog.csdn.net/chenmozhe22
序列化效果如下:
6.解码问题----中文、python2&python3不兼容等情况
def do_unserializing(file):
with open(file, "rb") as f:
obj = pickle.load(f, encoding='iso-8859-1')
创作不易,欢迎一键三连支持!!!
延伸阅读: 《python3中配置文件ini的使用详解----读写、list&dict&path等变量----configparser》 《Python3中打开文件的方式(With open)》
|