Python built-in module (ii)

Source: Internet
Author: User

-------------------"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)

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.