Python 18th days, python first day of the week

Source: Internet
Author: User

Python 18th days, python first day of the week

Learning content:

Json module, pickle module, and shelve Module

 

Json module serialization:

1 import json, pickle 2 3 info = {4 'name': 'A', 5 'age': 34, 6 'function': '7} 8 with open('text.txt ', 'W') as f: 9 f.write(json.dumps('test.txt ') # create a file for storing serialized data 10 f. write (json. dumps (info) # serialization

Deserialization of the json module (opening a file from another program ):

1 import json, pickle2 with open('text.txt ', 'R') as f: 3 4 fp = json. loads (f. read () # deserialization 5
# Fp = json. load (f) # same effect as loads
6 print('\033[32;1m%s\033[0m'%fp)

 

The usage of the pickle module. The method is the same as that of jsong (the difference is that pickle can store object formats, such as object attributes of functions that can store functions ):

1 def func1 (): 2 print ('000000') 3 info2 = {4 'name': 'A', 5 'age': 34, 6 'func ': func1 7 8} 9 with open ('test', 'wb') as f2: 10 f2.write (pickle. dumps (info2) # === pickle. dump (info2, f2) has the same effect

Deserialization of the pickle module:

1 def func1 (): 2 pass3 4 with open ('test', 'rb') as f2: 5 c = pickle. load (f2) 6 # c = pickle. loads (f2.read () # same effect as loads 7 8 print (c)

 

Shelve module (equivalent to multiple ldump, and load ):

Shelve uses the dictionary corresponding to the key and vaule to persist data through files-it can persist any python data formats supported by pickle:

1 import shelve 2 3 def func_1 (name, age): # define a function 4 print (name, age) 5 6 name = ['A', 'B ', 'C', 'D'] # define a list of 7 age = [1, 2, 3, 4] 8 9 d=shelve.open('test_3.txt ') # Use shelve to open the file 10 11 # Save each object to the file 12 d ['name'] = name13 d ['age'] = age14 d ['func'] = func_115 d. close ()

Shelve, deserialization:

1 import shelve 2 def func_1 (name, age): # define a function with the same name as the function stored in shelve 3 print (age) 4 f=shelve.open('test_3.txt ') # Open the previously stored data file 5 6 name = f ['name'] # extract the corresponding data name 7 print (name) 8 9 age = f ['age'] # extract the corresponding data age10 func_1 = f ['func'] # extract the corresponding data func_111 func_1 (name, age) # extract the corresponding data

 

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.