Python serialized JSON, Pickle

Source: Internet
Author: User
Tags serialization

#!/usr/bin/env python

#-*-Coding:utf-8-*-

# Author:changhua Gong


‘‘‘

JSON supports only simple data types and does not support serialization of complex types, such as functions

‘‘‘


Import JSON



info = {"Name": "Daidai", "Salary": 15000}

# serialization

With open ("Test.txt", "W") as F:

# F.write (Json.dumps (info))

Json.dump (info,f) # is exactly equivalent to the previous sentence

# Anti-serialization

With open ("Test.txt", "R") as F:

# Print (Json.loads (F.read ()))

Print (Json.load (f)) # is exactly equivalent to the previous sentence

#!/usr/bin/env python

#-*-Coding:utf-8-*-

# Author:changhua Gong

‘‘‘

Pickle can serialize all data types in Python, only in Python

For example, pickle serializable functions

‘‘‘


Import Pickle

Def fun_print ():

Print ("1 ...")


info = {"Name": "Daidai", "salary": 15000, "func": Fun_print}

# serialization

With open ("Test.txt", "WB") as F: # writes in binary

# F.write (Pickle.dumps (info))

Pickle.dump (info,f) # is exactly equivalent to the previous sentence




#!/usr/bin/env python

#-*-Coding:utf-8-*-

# Author:changhua Gong

Import Pickle


def fun_print (s):

Print ("2......%s"% s)

# Anti-serialization

With open ("Test.txt", "RB") as F: # read in binary

rs = Pickle.load (f)

Print (RS)

Rs["func"] ("Daidai") # actually there is a func corresponding function corresponding to (only need to ensure that the function name is consistent), and even modify the invocation mode and function body


This article is from the "90SirDB" blog, be sure to keep this source http://90sirdb.blog.51cto.com/8713279/1918517

Python serialized JSON, 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.