Python Learning path: Son and Pickle serialization

Source: Internet
Author: User
Tags serialization

Serialization Implementation Method:

#不实用模块 # info ={#     ' name ': ' Alex ', #     ' age ': 22##}# f = open ("Test.text", "W") # F.write (str (info)) # # f.close () #json Serialization simple data type # import json## info ={#     ' name ': ' Alex ', #     ' age ': 22##}# f = open ("Test.text", "W") # F.write (Json.dumps ( Info) # f.close () #python会跟JAVA, C language interaction, which requires an intermediate interactive language---json, can handle a simple data type # import json# def sayhi (name): #     Print (" Hello, ", name) # # info ={#     ' name ': ' Alex ', #     ' age ': 22,#     ' func ': sayhi##}# f = open (" Test.text "," W ") # F.write ( Json.dumps (info)) #报错, the function is not serializable import Pickle#pickle can serialize all data types in Python def sayhi (name):    print ("Hello,", name) info = {    ' name ': ' Alex ',    ' age ': $,    ' func ': sayhi}f = open ("Test.text", "WB") F.write (Pickle.dumps (info))

Deserialize the appropriate method:

# f = open ("Test.text", "R") # data = eval (F.read ()) # f.close () # # print (data[' age ') # import json## f = open ("Test.text", "R" # data = Json.loads (F.read ()) # # Print (data[' age ') #pickle只能在Python语言内部使用import pickle#f the entire data object to deserialize def sayhi (name):    print ("Hello,", name)    print ("Hello2,", name) F =open ("Test.text", "RB") Data =pickle.loads (F.read ()) #data = Pickle.load (f) Print (data["func"] (' Alex ')

python3.x can be json.dumps multiple times, but only once json.loads, it is recommended that each program only once json.dumps, once json.loads

Import jsoninfo ={    ' name ': ' Alex ',    ' age ': 22}f = open ("Test.text", "W") F.write (Json.dumps (info)) info[' age ']= 21f.write (Json.dumps (info)) F.close ()

Pickle.dumps and Pickle.dump;pickle.loads and Pickle.load

Import Pickle#pickle can serialize all data types in Python def sayhi (name):    print ("Hello,", name) info ={    ' name ': ' Alex ',    ' Age ': '    func ': sayhi}f = open ("Test.text", "WB") Pickle.dump (info,f) #f. Write (Pickle.dumps (info)) F.close ()---- ------------------------------------------------------------#pickle只能在Python语言内部使用import pickle#f Deserialization of the entire data object Def Sayhi (name): Print    ("Hello,", name)    print ("Hello2,", name) F =open ("Test.text", "RB") Data =pickle.loads ( F.read ()) #data =pickle.load (f) Print (data["func"] (' Alex ')

Python Learning path: Son and Pickle serialization

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.