Python Development Module Basics: Serialization Module Json,pickle,shelve

Source: Internet
Author: User

One, why serialize

# The process of converting an original dictionary, list, and other content into a string is called serialization
‘‘‘
For example, one of the data we calculate in Python code needs to be used for another program, so how do we give it?
Now we can think of a method that exists in the file, and then another Python program is read from the file.
But we all know that there is no dictionary concept for a file, so we can only convert the data into a dictionary and put it in a file.
You must ask, it is very simple to convert a dictionary into a string, that is, str (DIC) can do it, why do we have to learn the serialization module?
Yes, the process of serialization is the process of turning from DIC to STR (DIC). Now you can convert a dictionary called dic into a string with str (DIC),
But how do you convert a string into a dictionary?
You must have thought of it. Eval (), if we pass a dictionary str_dic of a string type to eval, we get a dictionary type that is returned.
The eval () function is very powerful, but what does eval do? E official demo is interpreted as: the string str as a valid expression to evaluate and return the results of the calculation.
but! Powerful functions are at the cost. Security is one of its biggest drawbacks.
Imagine that if we read from a file is not a data structure, but a "delete the file" similar to the destructive statement, then the consequences are not set to imagine.
The use of eval is a risk.
Therefore, we do not recommend using the Eval method for deserialization (converting STR to a data structure in Python)
‘‘‘
# Purpose of serialization
# 1. Persist the custom object in some form of storage;
# 2. Transfer objects from one place to another.
# 3, make the program more maintenance.
# Data Structure--serialization---STR
#-----deserialization---data structure

Two, the JSON module

JSON is not invented by Python, all languages are used, and languages are passed by string
A list of JSON string dictionaries can be
The JSON module provides four functions: Dumps,dump,loads,load

1 #!/usr/bin/env python2 #_*_coding:utf-8_*_3 4 #dictionary conversion to string5 ImportJSON6ret_s =Json.dumps (d)7 Print(Ret_s,type (ret_s))8 9 #Convert a string into a dictionaryTenRet_d =json.loads (ret_s) One Print(Ret_d,type (ret_d)) A  - #Note that the string must be a double quote, and the single quote will cause an error. -f = open ('Json_file') thed_s =F.read () - Print(Json.loads (d_s)) - f.close () -  + #Summary Memory Operations - #dumps structured data type to string + #loads string to structured data type all strings in a structured data type must be double quoted A  at #write back file dump load operation file with serialization relationship -f = open ('Json_file','W') -DIC = {'K1':'v1','K2':'v2','K3':'v3'} -Json.dump (DIC,F)#f is the file handle, and the DIC is written directly to the file. - #F.write (Json.dumps (DIC)) #是先dumps转成字符串在写入文件 - f.close () in  -f = open ('Json_file') toDic2 = Json.load (f)#just tell the file what it is . + Print(Dic2,type (DIC2))#is directly a dictionary type and does not require conversions -F.close ()

Three, pickle module

All data types are possible, but only Python-owned and bytes (for a unique Python state)

1 #!/usr/bin/env python2 #_*_coding:utf-8_*_3 4 #JSON & pickle Modules5 #two modules for serialization6     #json, used to convert between strings and Python data types (list, dictionary)7     #Pickle for conversion between Python-specific types and Python data types8 9 #The Pickle module provides four functions: dumps, dump (serialize, save), loads (deserialization, read), load (not only serializable dictionary, list ... Can serialize arbitrary data types in Python)Ten #Python's special ancestor will be made a list of JSON One #set JSON does not support ATu = {1,2,3,4} - #Import JSON - #print (Json.dumps (TU)) #json不支持, so this is not supported the ImportPickle - Print(Pickle.dumps (TU))#bytes Type -Pik_b =pickle.dumps (TU) - Print(Pickle.loads (Pik_b))#Turn back .

Four, shelve module

Shelve is also a serialization tool provided to us by Python, which is simpler to use than pickle.
Shelve only provides us with an open method, which is accessed using key, and is similar to the dictionary.

1 #!/usr/bin/env python2 #_*_coding:utf-8_*_3 4 Importshelve5f = Shelve.open ('Shelve_file')6f['Key'] = {'int': 10,'float': 9.5,'string':'Sample Data'}#directly to the file handle, you can deposit data7 f.close ()8 9 ImportshelveTenF1 = Shelve.open ('Shelve_file') Oneexisting = f1['Key']#when you take out the data, you just need to get it directly with key, but if key doesn't exist, it will be an error. A f1.close () - Print(existing) -  the #Note: This module has a limit, it does not support multiple applications at the same time to the same DB write operations.  - #So when we know that our application is only read, we can let shelve open the db by read-only mode . - ImportShelve#read-only mode, it won't affect the person who wrote it. -f = Shelve.open ('Shelve_file', flag='R') +existing = f['Key'] - f.close () + Print(existing) A  at #Note: Because shelve does not record any modifications to the persisted object by default, - #so we need to modify the default parameters at Shelve.open (), otherwise the modification of the object will not be saved.  - Importshelve -F1 = Shelve.open ('Shelve_file')#no change . - Print(f1['Key']) -f1['Key']['New_value'] ='This is not here before' in f1.close () -  toF2 = Shelve.open ('Shelve_file', writeback=true)#Change the + Print(f2['Key']) -f2['Key']['New_value'] ='This is not here before' the f2.close () *  $ #Summary: Writeback way has advantages and disadvantages. The advantage is that we reduce the probability of error, and make the object's persistence more transparent to the user;Panax Notoginseng #But this approach is not required in all cases, first, after using writeback, shelf will increase the additional memory consumption when open () , - #and when DB is in close (), each object in the cache is written to the DB, which also brings additional wait times.  the #because shelve has no way of knowing which objects in the cache have been modified and which objects have not been modified, all objects will be written. 

Python Development Module Basics: Serialization Module Json,pickle,shelve

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.