Python Common Modules

Source: Internet
Author: User

One, module, package

What is a module?

The module is essentially a Python file, which is used to organize the code, meaning that the Python code is written inside, the filename is the name of the module, test.py test is the module name.

What is a package?

Packages, the package is essentially a folder, and the folder is not the same is that it has a __init__.py file, the package is logic to organize the module, that is, it is used to store modules, if you want to import the modules in other directories, then this directory must be a package to be imported.

Import Module

The essence of the import module is to take the Python file and execute it once.

Using the modules in the package requires the from in the __init__.py file. Import xxx

Module Categories:

Standard library: Python built-in
Open Source module: third party
Custom module: Write it by yourself

Second, OS, sys module

Third, random module

Four, Time&datetime module

Time and datetime modules are primarily used for operation times

There are three ways to represent time, one is timestamp, one is format time, one is time tuple

Five, shelve module

Shelve module is used to persist data storage, compared to JSON, JSON can only store list, dictionary such data type, if it is a function, a class, there is no way to store, but shelve module can, shelve module is Key-value storage, Value is the content you store, using the following:

Vi.. Hashlib Module

Hashlib module, mainly used for encryption related operations, in the Python3 version, instead of the MD5 and SHA modules, mainly provides SHA1, SHA224, SHA256, SHA384, SHA512, MD5 algorithm.

Seven, Configparser module

The Configparser module is used to manipulate configuration files for generating and modifying common configuration documents, and Python 3.x is configparser in Configparser,python2.

A common configuration file is as follows:

What if you want to use Python to generate a document like this?

Here are some common operations, modifying, adding, deleting nodes, attributes

Eight, re module

The RE module is a regular expression module that is used to match some specific strings.

Common Regular Expression symbols

Common syntax for matching

Python Common Modules

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.