JSON module, pickle module (serialized) Shelve module

Source: Internet
Author: User

JSON module:

JSON: Information exchange (conversion of strings) to any language

After JSON conversion, all types can be converted to double quotation marks before being converted to STR

For example: file reads and writes:

1 #Write file:2 ImportJSON3f = open ("Demo.txt","W")4DIC = {"name":"g_l"}5Dic_str =json.dumps (DIC)6 f.write (DIC_STR)7 Print(Type (DIC_STR))8 Print(DIC_STR)
  
1 # Read file: 2 import JSON 3 f = open ( " Demo.txt ", " R " ) 4 f_read = F.read () 5 f_read = Json.loads (f_read) 6 print (type (f_read)) 7 print (Type (f_read))

Load and dump for file operations only: "DiC f reference above two code"

Json.dump (DIC,F)

Json.load (f)

Packle Modules: (Support functions, classes, etc.)

Serialization: The process of turning a variable (object) from memory into a storage or transfer is called serialization

1 ImportPickle2DIC = {"name":"Alex","name":"g_l"," Age": 18}3 #Print (Type (DIC))4j =pickle.dumps (DIC)5 Print(Type (j))#byte type6 Print(j)7f = open ("Serialized Object _pickle",'WB')8Pickle.dump (DIC,F)#f.write () #不可读文件9 f.close ()Ten ##---deserialization: One ImportPickle Af = open ("Serialized Object _pickle",'RB') -Date = Pickle.loads (F.read ())#---->date = Pickle.load (f) - Print(Type (date[" Age"])) the Print(date[" Age"])
View Code

Shelve module:

Compared to the Pickle module, it is simpler and has only one open function;

Returns an object similar to a dictionary;

Readable and writable;

Key must be a string;

Value is all the types that Python can support

JSON module, pickle module (serialized) Shelve module

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.