JSON and Pickle serialization modules

Source: Internet
Author: User

One, JSON serialization module

1. Serialization: The memory data is converted into a string to be saved.

2. Deserialization: The string is converted into memory data for reading.

data = {'Beijing':{                     'Five crossing':{                         'Sohu':'engine',                     }                   } }#Dumps <--> loads" "What is the meaning of turning the data type into a string into memory (json.dumps,json.loads)? 1. Remote sharing of your memory data to other people through the network 2, define the interaction rules between different languages <1> Plain text: cannot share complex data types <2> xml: Large space <3> JSON : Simple, good readability" "D= Json.dumps (data)#convert only to string <class ' str ' >Print(D,type (d))#{"\u5317\u4eac": {"\u4e94\u9053\u53e3": {"Sohu": "\u5f15\u64ce" }}}d_1= Json.loads (d)#Convert string to original data type <class ' Dict ' >Print(D_1,type (D_1))#{' Beijing ': {' Five crossings ': {' Sohu ': ' Engine ' }}}#Dump <--> Loadf = open ('Test.json','W')#first Open and then read and writeD2 = Json.dump (data,f)#turn into a string and write to a fileF1= Open ('Test.json','R') D_2= Json.load (F1)#convert a file to the original data typePrint(d_2)#{' Beijing ': {' Five crossings ': {' Sohu ': ' Engine ' }}}

Second, pickle serialization module

#! /usr/bin/env Python3#-*-coding:utf-8-*-#Write by CongcongImportpickledict= {'name':'cc',' Age': 21}len= ['python','Hello',' World']#Dumps <--> loadsD = Pickle.dumps (dict)#string converted to bytes typePrint(d)#B ' \x80\x03}q\x00 (x\x04\x00\x00\x00nameq\x01x\x02\x00\x00\x00ccq\x02x\x03\x00\x00\x00ageq\x03k\x15u. ' )D_1 = Pickle.loads (d)#bytes Type to original data type#print (d_1) # {' Name ': ' CC ', ' Age ': +}#Dump <--> Load#pk = open (' Data.pkl ', ' WB ')#D2 = Pickle.dump (DICT,PK) # string is converted to bytes type and saved to hard diskPk_r= Open ('DATA.PKL','RB')#Convert the bytes type in the hard drive to the original data typeD_2 =pickle.load (pk_r)Print(d_2)#{' name ': ' CC ', ' Age ': +}" "the difference between JSON and Pickle: JSON supports data types STR, int., tuple, list, dict, and can support all data types in Python in a cross-language pickle, but only in Python " "

JSON and Pickle serialization modules

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.