-------------------"Python Built-in module (i)"--------------------------------
Iv. JSON, pickle data serialization
two modules for serialization
JSON, used to convert between string and Python data types
Pickle for conversion 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.dumps converts data into a string that only Python can recognize in a special form
>>> Import Pickle
>>> data = {' lihongye1 ': ' pwd1 ', ' lihongye2 ': ' Pwd2 '}
>>> p_str = pickle.dumps (data)
>>> Print (P_STR)
or convert a string into a string that only Python can recognize. stored in file
Test = {' user ': ' Lihongye ', ' pwd ': ' pwd1 ', ' num ': ' 123 '}
Import Pickle
FW = open (' Test_file ', ' WB ')
Fw.write (Pickle.dumps (test))
Convert A string to the original data form via Pickle.loads
Import Pickle
FR = Open (' Test_file ', ' RB ')
FRP = Pickle.load (FR)
Print (FRP)
# You can also json.dumps data into a string format that all programs can recognize and write to a file
Test = {' user ': ' Lihongye ', ' pwd ': ' pwd1 ', ' num ': ' 123 '}
Import JSON
FW = open (' Test_file ', ' W ')
Fw.write (Json.dumps (test))
Convert a string to the original data format by using the Json.loads method
Import JSON
FR = Open (' Test_file ', ' R ')
FRP = Json.load (FR)
Print (FRP)
V. Random
Random number
>>> Import Random
>>> random.random () # Random decimals
0.7558096251338481
>>> Random.randint (1,3) # has a range of random numbers 1-3
2
>>> Random.randrange (1,10) # has a range of random numbers 1-9
1
Random Verification Code instances
Import Random
Check_code = ' '
For I in range (4):
Current = Random.randint (0,4)
If current! = I:
TMP = STR (chr (Random.randint (65,90)))
Else
TMP = Random.randint (0,9)
Check_code + = str (current)
Print (Check_code)
----------------------------------in the update------------------------------------
This article is from the "a myriad of simple pleasures of technology to enjoy" blog, please be sure to keep this source http://51enjoy.blog.51cto.com/8393791/1740927
Python built-in module (ii)