#!/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