Python:json & Pickle Data serialization

Source: Internet
Author: User
Tags serialization

Serialization:

#!usr/bin/env python
#-*-Coding:utf-8-*-
__author__ = "Samson"

Import Json,pickle
#json能用于其他语言中, only some simple data types, such as dictionaries, can be serialized, and pickle can only be used in Python to serialize all data types
def sayhi (name): #程序运行结束时会释放掉该内存
Print ("Name,", name)
info = {
"Name": "Alex",
"Age": 22,
"Func": sayhi# using JSON serialization is not possible, while serialization with Pickle is OK
}
f = open ("Test.text", "WB")
#print (Json.dumps (info))
#f. Write (Json.dumps (info)) #相当于json. Dump (info,f). * * Individual tried, if dump many times, then can not load () and loads (), preferably with dumps
F.write (Pickle.dumps (info)) #相当于pickle. Dump (info,f)
F.close ()

Deserialization:
#!usr/bin/env python
#-*-Coding:utf-8-*-
__author__ = "Samson"
Import Json,pickle
def sayhi (name): #必须有该函数名, otherwise unsuccessful
Print ("Name,", name)
Print ("name2,", name)
f = open ("Test.text", "RB")
#data = Json.loads (F.read ()) #相当于data2 = Json.load (f)
Data2 = Pickle.loads (F.read ()) #相当于data2 = Pickle.load (f)
F.close ()
Print (data2["func"] ("Alex"))

Python:json & Pickle Data serialization

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.