The serialization module of the Python module

Source: Internet
Author: User

Serialization of

"""     Sequence--string    serialization--Other data types are converted to string data type    deserialization--strings converted to other data types "" " 

JSON module

"""     JSON five-star Comment  number string list dictionary tuple        Advantages: Common serialization Format        disadvantage: Only a very small part of the data type----string "" "

  

1,dumps and loads

ImportJSON#dumps and loads manipulating the data in memoryDIC = {'K1':'v1'}Print(Type (DIC), DIC)#<class ' dict ' > {' K1 ': ' v1 '}Str_d = Json.dumps (DIC)#Serialization ofPrint(Type (str_d), str_d)#<class ' str ' > {"K1": "v1"}dic_l = Json.loads (str_d)#deserializationPrint(Type (dic_l), dic_l)#<class ' dict ' > {' K1 ': ' v1 '}

2,dump and load

#dump and load manipulate the data in the file to serialize the Chinese words, to modify the default parameters Ensure_ascii=falseDIC = {'K1':'v1'}f= Open ('json_test','W', encoding='Utf-8') Json.dump (DIC, F)#The contents of the file serialization are {"K1": "v1"}F.close () F= Open ('json_test', encoding='Utf-8')#load can only deserialize the contents of a one-time serialization into a file, and if multiple dump,load will be an error! RET =json.load (f)Print(ret)#{' K1 ': ' v1 '}F.close ()

JSON usage of some parameters

DIC = {'name':'蔠 Valerian',' Age': 25,'Hobby':'Read'}str_d= Json.dumps (DIC)#Serialization ofSTR_D1 = Json.dumps (dic, Ensure_ascii=false)#serialization Chinese to be able to read the content, to modify the parameters Ensure_ascii=falsePrint(Str_d)#{"Age": +, "name": "\u8520\u7f2c\u8349", "hobby": "Read"}Print(STR_D1)#{"Hobby": "read", "name": "蔠 Valerian", "Age":#serialization of special formatsDIC = {'name':'蔠 Valerian',' Age': 25,'Hobby':'Read'}str_d= Json.dumps (dic, Sort_keys=true, indent=4, separators= (',',':'), ensure_ascii=False)Print(str_d)#Print Content:"""{"Age": +, "hobby": "read", "name": "蔠 Valerian"}"""

  

Pickle Module

""" The pickle is serialized as a   bytes Type        advantage: All data types in Python--string        disadvantage: The serialized content only Python can recognize, serialization and deserialization need the same environment. """

The Pickle method is the same as the JSON method

ImportPickle#Pickle is the same as JSON, the difference is that the value after serialization is bytes typeDIC = {'K1':'v1','K2':'v2'}pd_str=pickle.dumps (DIC)Print(Pickle)#serialized Print bytes TypePl_str =pickle.loads (PD_STR)Print(PL_STR)#deserializationDIC = {'K1':'v1','K2':'v2'}f= Open ('pickle_test','WB') Pickle.dump (DIC, F)#serialization is written to the bytes typeF.close () F= Open ('pickle_test','RB') ret= Pickle.load (f)#deserializationPrint(ret)

Shelve module

"""     shelve the        existence        of a serialized handle using a handle directly, very convenient " " "

Shelve has only the open method, the open file returns a serialized handle, and the operation handle is used to complete the serialization operation.

ImportSHELVEF= Shelve.open ('shelve_test')#generating a serialized handlef['Key'] = {'K1':'v1','K2':'v2'}#manipulating handles for file serialization writesF.close () F= Shelve.open ('shelve_test')Print(f['Key'])#deserialization value, key must exist, otherwise errorF.close ()

Shelve the use of writeback parameters

f = Shelve.open ('shelve_test', Writeback=false)#parameter Writeback=false data modification does not take effectf['Key']['K3'] ='v3'  #The modification did not take effectF.close () F= Shelve.open ('shelve_test')Print(f['Key'])#{' K2 ': ' v2 ', ' K1 ': ' v1 '}F.close () F= Shelve.open ('shelve_test', writeback=True) f['Key']['K3'] ='v3'  #changes take effectF.close () F= Shelve.open ('shelve_test')Print(f['Key'])#{' K3 ': ' V3 ', ' K2 ': ' v2 ', ' K1 ': ' v1 '}F.close ()

The serialization module of the Python module

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.