Python serialization and deserialization of-json and pickle

Source: Internet
Author: User
Tags serialization

Python serialization and deserialization of-json and pickle

Yun Zhengjie

Copyright Notice: Original works, declined reprint! Otherwise, the legal liability will be investigated.

I. How JSON is serialized and deserialized

Serialization of 1>.json

1 #! /usr/bin/env python2 #_*_coding:utf-8_*_3 #@author: Yinzhengjie4 #blog:http://www.cnblogs.com/yinzhengjie/tag/python%e8%87%aa%e5%8a%a8%e5%8c%96%e8%bf%90%e7%bb%b4%e4%b9%8b% e8%b7%af/5 #Email:[email protected]6 7 8 """9 I. What is serialization:Ten The process of changing an object (variable) from memory to a storage or transfer is called serialization, called picking in Python, and in other languages One called serialization,marshalling,flattening and so on, all is a meaning. After serialization, the serialized content can be written to disk, A or through the network to other machines, in turn, the variable from the content from the serialized object re-supervised in memory called deserialization, that is, unpicking.  -  - two. The relationship between serialization and encoding the serialization is the operation that is performed before encoding. Serialization is the uniform processing of various data types into strings, which need to be specified after serialization is complete - how do strings (e.g., UTF-8,GBK, etc.) be stored in the hard disk? Of course in deserializing the contents of a file is that the first thing to do is decode, - That is , in which case the file is opened, and if the encoding format is turned on, the correct string is obtained so that the deserialization can be done.  -  + three. JSON module - if we were to pass objects between different programming languages, we had to serialize the objects into standard formats, such as XML, but a better approach would be to serialize them to JSON. + because JSON represents a string that can be read by all languages, it can be easily stored on disk or transmitted over a network. JSON is not just a standard format, A and faster than XML, and can be read directly in the Web page, very convenient.  at  - """ -  - ImportJSON -  -accounts = { in     "Name":"Yinzhengjie", -     "ID":" About", to     "banlance":"20000", + } -  the #serialization mode one: * #f = open (r "E:\Code\pycharm\ file storage \python Learning Note \day11\yinzhengjie.txt", "W") #打开文件 $ #Panax Notoginseng #json_str = json.dumps (accounts) #序列化操作 - # the #f.write (json_str) #写入文件 +  A #serialization mode two: theWith open (r"E:\Code\pycharm\ File Repository \python study notes \day11\yinzhengjie.txt","W") as F: +Json.dump (ACCOUNTS,F)
1 {"Name""Yinzhengjie" "ID"  "banlance" "20000 "}
yinzhengjie.txt File Contents

2> Deserialize the method of deserialization

1 #! /usr/bin/env python2 #_*_coding:utf-8_*_3 #@author: Yinzhengjie4 #blog:http://www.cnblogs.com/yinzhengjie/tag/python%e8%87%aa%e5%8a%a8%e5%8c%96%e8%bf%90%e7%bb%b4%e4%b9%8b% e8%b7%af/5 #Email:[email protected]6 7 ImportJSON8 9With open (r"E:\Code\pycharm\ File Repository \python study notes \day11\yinzhengjie.txt") as F:Ten     Print(Json.load (f))

Two. Serialization and deserialization of pickle

1> serialization mode

1 #! /usr/bin/env python2 #_*_coding:utf-8_*_3 #@author: Yinzhengjie4 #blog:http://www.cnblogs.com/yinzhengjie/tag/python%e8%87%aa%e5%8a%a8%e5%8c%96%e8%bf%90%e7%bb%b4%e4%b9%8b% e8%b7%af/5 #Email:[email protected]6 7 ImportPickle8 9 " "Ten Pickle: One 1.> is used to convert between Python-specific types and Python data types A the 2.>pickle module provides four functions: dumps, dump, loads, load. - Note: Converting data into a string that is only recognized by the Python interpreter in a special form is called serialization, and what Python-recognized strings are converted to what we can read is called deserialization.  - " " the  -  -Datainfo = { -     "Name":"Yun Zhengjie", +     "Password":"123" - } +  A  at  - ##将数据通过特殊的形式转换为只有python语言知识的字符串并写入文件 - #pickle_str = pickle.dumps (datainfo) - #print (PICKLE_STR) - #f = open ("Yinzhengjie.txt", "WB") - #f.write (PICKLE_STR) in  - #The above method of writing to the file can also be played so that it looks more simple toWith open ("Yinzhengjie.txt","WB") as F: +Pickle.dump (DATAINFO,F)

2. Deserialization mode

1 #! /usr/bin/env python2 #_*_coding:utf-8_*_3 #@author: Yinzhengjie4 #blog:http://www.cnblogs.com/yinzhengjie/tag/python%e8%87%aa%e5%8a%a8%e5%8c%96%e8%bf%90%e7%bb%b4%e4%b9%8b% e8%b7%af/5 #Email:[email protected]6 7 ImportPickle8 9 " "Ten two modules for serialization One 1>.json: Used to convert between string and Python data types A 2>.pickle: Converting between Python-specific types and Python data types - The JSON module provides four functions: dumps, dump, loads, load - The Pickle module provides four functions: dumps, dump, loads, load the " " -  -  -f = open ("Yinzhengjie.txt","RB") +  - #Print (Pickle.loads (F.read () )) #loads方式反序列化 +  A Print(Pickle.load (f))#The load method is deserialized

Three. Compare the similarities and differences between JSON and pickle:

1> The same point: all modules used for serialization and deserialization.

2>: JSON is a data storage format common to all languages, and pickle is only a storage format unique to the Python language.

Python serialization and deserialization of-json and pickle

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.