pickle模块与json模块一样,都是可以实现对数据的序列化与反序列化,但是json的模块是通用的,pickle的模块是
python语言中特有的,在学习一个模块的时候,我们先看看该模块的方法以及help,具体为:
#!/usr/bin/env python #-*- coding:utf-8 -*- import pickle print u'查看piclle所使用的方法:', dir(pickle) print u'查看pickle模块额度详细帮助信息:',type(help(pickle))
见执行上的代码后,输出的内容:
C:\Python27\python.exe D:/git/Python/FullStack/share/pickleTest1.py 查看piclle所使用的方法: ['APPEND', 'APPENDS', 'BINFLOAT', 'BINGET', 'BININT', 'BININT1', 'BININT2', 'BINPERSID', 'BINPUT', 'BINSTRING', 'BINUNICODE', 'BUILD', 'BooleanType', 'BufferType', 'BuiltinFunctionType', 'BuiltinMethodType', 'ClassType', 'CodeType', 'ComplexType', 'DICT', 'DUP', 'DictProxyType', 'DictType', 'DictionaryType', 'EMPTY_DICT', 'EMPTY_LIST', 'EMPTY_TUPLE', 'EXT1', 'EXT2', 'EXT4', 'EllipsisType', 'FALSE', 'FLOAT', 'FileType', 'FloatType', 'FrameType', 'FunctionType', 'GET', 'GLOBAL', 'GeneratorType', 'GetSetDescriptorType', 'HIGHEST_PROTOCOL', 'INST', 'INT', 'InstanceType', 'IntType', 'LIST', 'LONG', 'LONG1', 'LONG4', 'LONG_BINGET', 'LONG_BINPUT', 'LambdaType', 'ListType', 'LongType', 'MARK', 'MemberDescriptorType', 'MethodType', 'ModuleType', 'NEWFALSE', 'NEWOBJ', 'NEWTRUE', 'NONE', 'NoneType', 'NotImplementedType', 'OBJ', 'ObjectType', 'PERSID', 'POP', 'POP_MARK', 'PROTO', 'PUT', 'PickleError', 'Pickler', 'PicklingError', 'PyStringMap', 'REDUCE', 'SETITEM', 'SETITEMS', 'SHORT_BINSTRING', 'STOP', 'STRING', 'SliceType', 'StringIO', 'StringType', 'StringTypes', 'TRUE', 'TUPLE', 'TUPLE1', 'TUPLE2', 'TUPLE3', 'TracebackType', 'TupleType', 'TypeType', 'UNICODE', 'UnboundMethodType', 'UnicodeType', 'Unpickler', 'UnpicklingError', 'XRangeType', '_EmptyClass', '_Stop', '__all__', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '__version__', '_binascii', '_extension_cache', '_extension_registry', '_inverted_registry', '_keep_alive', '_test', '_tuplesize2code', 'classmap', 'compatible_formats', 'decode_long', 'dispatch_table', 'dump', 'dumps', 'encode_long', 'format_version', 'load', 'loads', 'marshal', 'mloads', 're', 'struct', 'sys', 'whichmodule'] 查看pickle模块额度详细帮助信息:Help on module pickle: NAME pickle - Create portable serialized representations of Python objects. FILE c:\python27\lib\pickle.py DESCRIPTION See module cPickle for a (much) faster implementation. See module copy_reg for a mechanism for registering custom picklers. See module pickletools source for extensive comments. Classes: Pickler Unpickler Functions: dump(object, file) dumps(object) -> string load(file) -> object loads(string) -> object Misc variables: __version__ format_version compatible_formats CLASSES exceptions.Exception(exceptions.BaseException) PickleError PicklingError UnpicklingError Pickler Unpickler class PickleError(exceptions.Exception) | A common base class for the other pickling exceptions. | | Method resolution order: | PickleError | exceptions.Exception | exceptions.BaseException | __builtin__.object | | Data descriptors defined here: | | __weakref__ | list of weak references to the object (if defined) | | ---------------------------------------------------------------------- | Methods inherited from exceptions.Exception: | | __init__(...) | x.__init__(...) initializes x; see help(type(x)) for signature | | ---------------------------------------------------------------------- | Data and other attributes inherited from exceptions.Exception: | | __new__ = <built-in method __new__ of type object> | T.__new__(S, ...) -> a new object with type S, a subtype of T | | ---------------------------------------------------------------------- | Methods inherited from exceptions.BaseException: | | __delattr__(...) | x.__delattr__('name') <==> del x.name | | __getattribute__(...) | x.__getattribute__('name') <==> x.name | | __getitem__(...) | x.__getitem__(y) <==> x[y] | | __getslice__(...) | x.__getslice__(i, j) <==> x[i:j] | | Use of negative indices is not supported. | | __reduce__(...) | | __repr__(...) | x.__repr__() <==> repr(x) | | __setattr__(...) | x.__setattr__('name', value) <==> x.name = value | | __setstate__(...) | | __str__(...) | x.__str__() <==> str(x) | | __unicode__(...) | | ---------------------------------------------------------------------- | Data descriptors inherited from exceptions.BaseException: | | __dict__ | | args | | message class Pickler | Methods defined here: | | __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. | | Specifying a negative protocol version selects the highest | protocol version supported. The higher the protocol used, the | more recent the version of Python needed to read the pickle | produced. | | The file parameter must have a write() method that accepts a single | string argument. It can thus be an open file object, a StringIO | object, or any other custom object that meets this interface. | | clear_memo(self) | Clears the pickler's "memo". | | The memo is the data structure that remembers which objects the | pickler has already seen, so that shared or recursive objects are | pickled by reference and not by value. This method is useful when | re-using picklers. | | dump(self, obj) | Write a pickled representation of obj to the open file. | | get(self, i, pack=<built-in function pack>) | # Return a GET (BINGET, LONG_BINGET) opcode string, with argument i. | | memoize(self, obj) | Store an object in the memo. | | persistent_id(self, obj) | | put(self, i, pack=<built-in function pack>) | # Return a PUT (BINPUT, LONG_BINPUT) opcode string, with argument i. | | save(self, obj) | | save_bool(self, obj) | | save_dict(self, obj) | | save_empty_tuple(self, obj) | # save_empty_tuple() isn't used by anything in Python 2.3. However, I | # found a Pickler subclass in Zope3 that calls it, so it's not harmless | # to remove it. | | save_float(self, obj, pack=<built-in function pack>) | | save_global(self, obj, name=None, pack=<built-in function pack>) | | save_inst(self, obj) | | save_int(self, obj, pack=<built-in function pack>) | | save_list(self, obj) | | save_long(self, obj, pack=<built-in function pack>) | | save_none(self, obj) | | save_pers(self, pid) | | save_reduce(self, func, args, state=None, listitems=None, dictitems=None, obj=None) | | save_string(self, obj, pack=<built-in function pack>) | | save_tuple(self, obj) | | save_unicode(self, obj, pack=<built-in function pack>) | | ---------------------------------------------------------------------- | Data and other attributes defined here: | | dispatch = {<type 'unicode'>: <function save_unicode>, <type 'type'>: ... class PicklingError(PickleError) | This exception is raised when an unpicklable object is passed to the | dump() method. | | Method resolution order: | PicklingError | PickleError | exceptions.Exception | exceptions.BaseException | __builtin__.object | | Data descriptors inherited from PickleError: | | __weakref__ | list of weak references to the object (if defined) | | ---------------------------------------------------------------------- | Methods inherited from exceptions.Exception: | | __init__(...) | x.__init__(...) initializes x; see help(type(x)) for signature | | ---------------------------------------------------------------------- | Data and other attributes inherited from exceptions.Exception: | | __new__ = <built-in method __new__ of type object> | T.__new__(S, ...) -> a new object with type S, a subtype of T | | ---------------------------------------------------------------------- | Methods inherited from exceptions.BaseException: | | __delattr__(...) | x.__delattr__('name') <==> del x.name | | __getattribute__(...) | x.__getattribute__('name') <==> x.name | | __getitem__(...) | x.__getitem__(y) <==> x[y] | | __getslice__(...) | x.__getslice__(i, j) <==> x[i:j] | | Use of negative indices is not supported. | | __reduce__(...) | | __repr__(...) | x.__repr__() <==> repr(x) | | __setattr__(...) | x.__setattr__('name', value) <==> x.name = value | | __setstate__(...) | | __str__(...) | x.__str__() <==> str(x) | | __unicode__(...) | | ---------------------------------------------------------------------- | Data descriptors inherited from exceptions.BaseException: | | __dict__ | | args | | message class Unpickler | Methods defined here: | | __init__(self, file) | This takes a file-like object for reading a pickle data stream. | | The protocol version of the pickle is detected automatically, so no | proto argument is needed. | | The file-like object must have two methods, a read() method that | takes an integer argument, and a readline() method that requires no | arguments. Both methods should return a string. Thus file-like | object can be a file object opened for reading, a StringIO object, | or any other custom object that meets this interface. | | find_class(self, module, name) | | get_extension(self, code) | | load(self) | Read a pickled object representation from the open file. | | Return the reconstituted object hierarchy specified in the file. | | load_append(self) | | load_appends(self) | | load_binfloat(self, unpack=<built-in function unpack>) | | load_binget(self) | | load_binint(self) | | load_binint1(self) | | load_binint2(self) | | load_binpersid(self) | | load_binput(self) | | load_binstring(self) | | load_binunicode(self) | | load_build(self) | | load_dict(self) | | load_dup(self) | | load_empty_dictionary(self) | | load_empty_list(self) | | load_empty_tuple(self) | | load_eof(self) | | load_ext1(self) | | load_ext2(self) | | load_ext4(self) | | load_false(self) | | load_float(self) | | load_get(self) | | load_global(self) | | load_inst(self) | | load_int(self) | | load_list(self) | | load_long(self) | | load_long1(self) | | load_long4(self) | | load_long_binget(self) | | load_long_binput(self) | | load_mark(self) | | load_newobj(self) | | load_none(self) | | load_obj(self) | | load_persid(self) | | load_pop(self) | | load_pop_mark(self) | | load_proto(self) | | load_put(self) | | load_reduce(self) | | load_setitem(self) | | load_setitems(self) | | load_short_binstring(self) | | load_stop(self) | | load_string(self) | | load_true(self) | | load_tuple(self) | | load_tuple1(self) | | load_tuple2(self) | | load_tuple3(self) | | load_unicode(self) | | marker(self) | # Return largest index k such that self.stack[k] is self.mark. | # If the stack doesn't contain a mark, eventually raises IndexError. | # This could be sped by maintaining another stack, of indices at which | # the mark appears. For that matter, the latter stack would suffice, | # and we wouldn't need to push mark objects on self.stack at all. | # Doing so is probably a good thing, though, since if the pickle is | # corrupt (or hostile) we may get a clue from finding self.mark embedded | # in unpickled objects. | | ---------------------------------------------------------------------- | Data and other attributes defined here: | | dispatch = {'': <function load_eof>, '(': <function load_mark>, ')': <... class UnpicklingError(PickleError) | This exception is raised when there is a problem unpickling an object, | such as a security violation. | | Note that other exceptions may also be raised during unpickling, including | (but not necessarily limited to) AttributeError, EOFError, ImportError, | and IndexError. | | Method resolution order: | UnpicklingError | PickleError | exceptions.Exception | exceptions.BaseException | __builtin__.object | | Data descriptors inherited from PickleError: | | __weakref__ | list of weak references to the object (if defined) | | ---------------------------------------------------------------------- | Methods inherited from exceptions.Exception: | | __init__(...) | x.__init__(...) initializes x; see help(type(x)) for signature | | ---------------------------------------------------------------------- | Data and other attributes inherited from exceptions.Exception: | | __new__ = <built-in method __new__ of type object> | T.__new__(S, ...) -> a new object with type S, a subtype of T | | ---------------------------------------------------------------------- | Methods inherited from exceptions.BaseException: | | __delattr__(...) | x.__delattr__('name') <==> del x.name | | __getattribute__(...) | x.__getattribute__('name') <==> x.name | | __getitem__(...) | x.__getitem__(y) <==> x[y] | | __getslice__(...) | x.__getslice__(i, j) <==> x[i:j] | | Use of negative indices is not supported. | | __reduce__(...) | | __repr__(...) | x.__repr__() <==> repr(x) | | __setattr__(...) | x.__setattr__('name', value) <==> x.name = value | | __setstate__(...) | | __str__(...) | x.__str__() <==> str(x) | | __unicode__(...) | | ---------------------------------------------------------------------- | Data descriptors inherited from exceptions.BaseException: | | __dict__ | | args | | message FUNCTIONS dump(obj, file, protocol=None) dumps(obj, protocol=None) load(file) loads(str) DATA APPEND = 'a' APPENDS = 'e' BINFLOAT = 'G' BINGET = 'h' BININT = 'J' BININT1 = 'K' BININT2 = 'M' BINPERSID = 'Q' BINPUT = 'q' BINSTRING = 'T' BINUNICODE = 'X' BUILD = 'b' DICT = 'd' DUP = '2' EMPTY_DICT = '}' EMPTY_LIST = ']' EMPTY_TUPLE = ')' EXT1 = '\x82' EXT2 = '\x83' EXT4 = '\x84' FALSE = 'I00\n' FLOAT = 'F' GET = 'g' GLOBAL = 'c' HIGHEST_PROTOCOL = 2 INST = 'i' INT = 'I' LIST = 'l' LONG = 'L' LONG1 = '\x8a' LONG4 = '\x8b' LONG_BINGET = 'j' LONG_BINPUT = 'r' MARK = '(' NEWFALSE = '\x89' NEWOBJ = '\x81' NEWTRUE = '\x88' NONE = 'N' OBJ = 'o' PERSID = 'P' POP = '0' POP_MARK = '1' PROTO = '\x80' PUT = 'p' REDUCE = 'R' SETITEM = 's' SETITEMS = 'u' SHORT_BINSTRING = 'U' STOP = '.' STRING = 'S' TRUE = 'I01\n' TUPLE = 't' TUPLE1 = '\x85' TUPLE2 = '\x86' TUPLE3 = '\x87' UNICODE = 'V' __all__ = ['PickleError', 'PicklingError', 'UnpicklingError', 'Pickler... __version__ = '$Revision: 72223 $' VERSION 72223 <type 'NoneType'> Process finished with exit code 0
其实,pickle经常使用的方法就四个,其他的可以忽略,具体为:dumps(),dump(),loads(),load()。下面通过具体的例子来
说明这四个方法的使用。
我们先来进行序列化和反序列化的说明,序列化就是把python的数据类型转为json的字符串,而反序列化就是把json的字符串
类型转为python的基本数据类型。我们先来看如下的序列化的过程,见实现的代码:
#!/usr/bin/env python #-*- coding:utf-8 -*- import pickle accounts={ 10: { 'name':'wuya', 'mail':'wuyaf@outlook.com', 'password':'password', 'balance':1500, 'phone':'134XXXXXXXX' } } account_dumps=pickle.dumps(accounts) print u'查看序列化后的内容:\n',account_dumps,u'查看序列化后的数据类型:\n',type(account_dumps)
见执行后输出的内容:
C:\Python27\python.exe D:/git/Python/FullStack/share/pickleTest1.py 查看序列化后的内容: (dp0 I10 (dp1 S'mail' p2 S'wuyaf@outlook.com' p3 sS'password' p4 g4 sS'name' p5 S'wuya' p6 sS'balance' p7 I1500 sS'phone' p8 S'134XXXXXXXX' p9 ss. 查看序列化后的数据类型: <type 'str'>
可以看到,经过序列化后,数据类型是str,但是输出的内容确实看不懂,其实这是ASCll的数据格式,如果序列化的时候填写True,是以
二进制的格式输出的,见修改后的代码和输出的内容:
#!/usr/bin/env python #-*- coding:utf-8 -*- import pickle accounts={ 10: { 'name':'wuya', 'mail':'wuyaf@outlook.com', 'password':'password', 'balance':1500, 'phone':'134XXXXXXXX' } } account_dumps=pickle.dumps(accounts,True) print u'查看序列化后的内容:\n',account_dumps,u'查看序列化后的数据类型:\n',type(account_dumps)
执行如上的代码后,就会以二进制的数据格式输出,见内容:
C:\Python27\python.exe D:/git/Python/FullStack/share/pickleTest1.py 查看序列化后的内容: }q K }q(UmailqUwuyaf@outlook.comqpasswordqhUnameqUwuyaqUbalanceqM�UphoneU134XXXXXXXXq us. 查看序列化后的数据类型: <type 'str'>
下面我们来进行反序列化,也就是把str的数据类型转为python的数据类型,见实现的代码:
#!/usr/bin/env python #-*- coding:utf-8 -*- import pickle accounts={ 10: { 'name':'wuya', 'mail':'wuyaf@outlook.com', 'password':'password', 'balance':1500, 'phone':'134XXXXXXXX' } } account_dumps=pickle.dumps(accounts,True) #进行反序列化 account_loads=pickle.loads(account_dumps) print u'反序列化后的内容:{0}'.format(account_loads),u'数据类型为:{0}'.format(type(account_loads))
见输出的内容:
C:\Python27\python.exe D:/git/Python/FullStack/share/pickleTest1.py 反序列化后的内容:{10: {'mail': 'wuyaf@outlook.com', 'password': 'password', 'name': 'wuya', 'balance': 1500, 'phone': '134XXXXXXXX'}} 数据类型为:<type 'dict'> Process finished with exit code 0
如上的内容可以看到,数据类型是dict,也就是字典。
在现实的例子中,或者说是在实际中,序列化和反序列化,是以文件的形式进行的,比如读取文件的内容然后进行序列化这样的
处理,而不是我们如上这样简单的内容,下面就来看文件的序列化的处理,见代码:
#!/usr/bin/env python #-*- coding:utf-8 -*- import pickle accounts={ 10: { 'name':'wuya', 'mail':'wuyaf@outlook.com', 'password':'password', 'balance':1500, 'phone':'13484545195' } } #把accounts内容写到文件中 accounts_dump=pickle.dumps(accounts) with open('pickle.md','wb') as f: f.write(accounts_dump) #对文件的内容进行反序列化 accounts_file=open('pickle.md','rb') account_dict=accounts_file.read() print u'反序列化后的文件内容:\n',pickle.loads(account_dict)
对文件的内容进行修改,修改后,把修改的内容写入到文件中,在进行反序列化的操作,见实现的代码:
#!/usr/bin/env python #-*- coding:utf-8 -*- import pickle accounts={ 10: { 'name':'wuya', 'mail':'wuyaf@outlook.com', 'password':'password', 'balance':1500, 'phone':'13484545195' } } #把accounts内容写到文件中 accounts_dump=pickle.dumps(accounts) with open('pickle.md','wb') as f: f.write(accounts_dump) #对文件的内容进行反序列化 accounts_file=open('pickle.md','rb') account_dict=accounts_file.read() fileContent=pickle.loads(account_dict) print u'读取文件的内容:\n',fileContent #对文件的内容进行修改 fileContent[10]['balance']=-500 #把修改后的文件内容写入到文件中 with open('pickle.md','wb') as f: f.write(pickle.dumps(fileContent)) #对修改后的文件进行反序列化 with open('pickle.md','rb') as f: f2=f.read() print u'修改后的文件进行序列化后的内容为:\n',pickle.loads(f2)
下面来看dump()与load()方法的使用,看对文件进行如何的操作,见实现的代码:
#!/usr/bin/env python #-*- coding:utf-8 -*- import pickle def writeFile(): accounts = { 10: { 'name': 'wuya', 'mail': 'wuyaf@outlook.com', 'password': 'password', 'balance': 1500, 'phone': '13484545195' } } # 把accounts内容写到文件中 accounts_dump = pickle.dumps(accounts) with open('pickle.md', 'wb') as f: f.write(accounts_dump) def f1(): '''使用dump()方法对文件直接写进去''' writeFile() with open('pickle.md','rb') as f: #读取文件的内容 fileContent=f.read() account_dic=pickle.loads(fileContent) #对文件的内容进行修改 account_dic[10]['balance']=-100 with open('pickle.md','wb') as f: pickle.dump(account_dic,f) print u'数据类型为:',type(pickle.dump(account_dic,f)) def f2(): '''load()方法不需要read()方法,直接读取文件就可以了''' with open('pickle.md','rb') as f: account_dict1=pickle.load(f) print u'文件的内容为:\n',account_dict1