Tag: The support method uses the title floating point Targe sci xPL and
Two modules for serialization
JSON: Used to convert between string and Python data types
Pickle: Converting between Python-specific types and Python data types
JSON offers four features: Dumps,dump,loads,load
Pickle offers four functions: Dumps,dump,loads,load
What type of data can pickle store?
- All Python-supported native types: Boolean, Integer, floating-point, plural, string, byte, None.
- Lists, tuples, dictionaries, and collections made up of any native type.
- Instances of functions, classes, classes
the methods commonly used in pickle modules are:
1. Pickle.dump (obj, file, Protocol=none,)
Required parameter obj represents the object that will be encapsulated
The required parameter file represents the object to which obj is to be written, which must be opened in binary writable mode, the "WB"
The optional parameter protocol indicates that the protocol used by the Pickler is notified, that the supported protocols are 0,1,2,3, and that the default protocol is to add protocol 3 in Python 3.
- Protocol version 0 is the original "human-readable" Protocol and are backwards compatible with earlier versions of Python.
- Protocol version 1 is a old binary format which was also compatible with earlier versions of Python.
- Protocol version 2 is introduced in Python 2.3. It provides much more efficient pickling of New-style classes. Refer to PEP 307 For information on improvements brought by protocol 2.
- Protocol version 3 is added in Python 3.0. It has explicit support for bytes objects and cannot is unpickled by Python 2.x. This is the default protocol, and the recommended protocol if compatibility with other Python 3 versions is required.
- Protocol version 4 is added in Python 3.4. It adds support for very large objects, pickling more kinds of objects, and some data format optimizations. Refer to PEP 3154 For information on improvements brought by protocol 4.
2. Pickle.load (File,*,fix_imports=true, encoding= "ASCII", errors= "strict")
Required parameter file must be opened in binary readable mode, "RB", other optional parameters
3. Pickle.dumps (OBJ): Returns encapsulated object as a byte object without writing to the file
4. Pickle.loads (Bytes_object): reads the encapsulated object from the byte object and returns
There are three types of exceptions that can occur with the Pickle module:
1. Pickleerror: Exception classes that occur when encapsulating and unpacking, inherited from exception
2. Picklingerror: Exception that occurs when a non-encapsulated object is encountered, inherited from Pickleerror
3. Unpicklingerror: An exception occurred during the unpacking of the object, inherited from Pickleerror
Application:
1 # Dumps function 2 import pickle3 data = [' AA ', ' BB ', ' cc '] 4 # Dumps converts the data through special forms to only the Python-language-aware string 5 P_str = Pickle.dumps (da TA) 6 print (P_STR)
7 B ' \x80\x03]q\x00 (x\x02\x00\x00\x00aaq\x01x\x02\x00\x00\x00bbq\x02x\x02\x00\x00\x00ccq\x03e.
1 # Loads feature 2 # loads convert pickle data to Python's structure 3 mes = Pickle.loads (p_str) 4 print (MES) 5 [' AA ', ' BB ', ' CC ']
1 # Dump Function 2 # Dump converts data into a string that is known only in the Python language in a special form and writes the file 3 with open (' d:/tmp.pk ', ' W ') as F:4 pickle.dump (data, F)
1 # Load Function 2 # Load reads data from data file and translates to Python data structure 3 with open (' d:/tmp.pk ', ' R ') as F:4 data = Pickle.load (f)
Use of the Python pickle module