Two modules for serialization
JSON: Used to convert between string and Python data types
Pickle: Converting between Python-specific types and Python data types
The JSON module provides four functions: dumps dump loads load
The Pickle module provides four functions: dumps dump loads load
Pickle instances:
Import Pickle
data = {' K1 ': 123, ' K2 ': ' Hello '}
Pickle. Dumps converts data to a string that can be recognized only by Python speech in a special form
Pickle. dumps (data)
Print P_str
6: "'
7: Print the result:
8: (dp0
9:s ' K2 '
10:p1
11:s ' Hello '
12:p2
13:ss ' K1 '
14:p3
15:i123
16:s.
17: "'
Pickle. dump transforms the data into a string that is known only to the Python voice in a special form and writes the file
Open (' e:/python/tmp/result.pk ', ' W ') as FP:
: pickle. Dump(data, FP)
JSON instance
Import JSON
data = {' K1 ': 123, ' K2 ': ' Hello '}
3: # JSON. dumps converts data into strings that are known to all programming languages in a special form
4:J_STR = json. dumps (data)
Print J_str
6: # Print Result: {"K2": "Hello", "K1": 123}
7: # JSON. dump transforms the data into a string that is known to all languages in a special form and writes the file
Open (' e:/python/tmp/result.pk ', ' W ') as JP:
9: JSON. Dump(data, JP)
Configparser
1: #!/usr/bin/env python
2: #-*-Coding:utf-8-*-
3: # to operate on a specific configuration, the name of the current module is changed to Configparser in the Python 3.x version.
Import Configparser
5:config = Configparser.configparser ()
6:config. Read (' Goods.txt ')
8: # Get the name of the module
9:secs = Config.sections ()
Print secs
11: # Result: [' section1 ', ' Section2 ']
12: # Gets the key value of the specified module
13:options = config.options (' Section1 ')
Print Options
15: # Result: [' K1 ', ' K2 ']
16: # Gets the items under the specified module
17:item_list = Config.items (' Section1 ')
Print item_list
19: # Result: [(' K1 ', ' v1 '), (' K2 ', ' V2 ')]
20: # Gets the value of the key under the specified module
21:val = Config.get (' Section1 ', ' K2 ')
Print val
JSON and Pickle