Python Standard library Overview

Source: Internet
Author: User
Tags mathematical functions time and date

The Python standard library, often referred to as the "own battery", naturally offers a wide range of functions, covering about 200 packages and modules. Continuous high-quality packages or modules have been developed, greatly enriching the standard library. However, some modules are difficult to maintain in the standard library, such as the "Berkeley DB" module, which is cleaned out of the standard library for individual maintenance. Some libraries, such as pyparsing (create parser), are also not added to the standard library.

1 string processing

1.1 String Module

Constants: String.ascii_letters, String.hexdigits.

String. Formatter subclass, custom string formatter. The TextWrap module can be used to capture text lines of a specified width and minimize the need for indentation.

1.2 struct MODULE

Provides functions for packaging numeric Boolean variables and strings as byte objects (in binary notation), or splitting them from byte objects into appropriate types for interacting with the underlying C language.

1.3 Difflib Module

Classes and methods for comparing sequences (such as strings) are provided, and output information in the standard "diff" format and HTML format can be produced.

1.4 Re-regular expression

The most powerful string processing module

2 io. Stringio class

Python provides two ways to write text to a file, one that uses the Open,write method, a print () function, and sets its keyword parameter file to open and wait for the write to be written. Like what:

    

Print ("An error message", file = sys.stdout) sys.stdout.write ("Another error message\n")

The above two lines of text will be printed out to Sys.stdout, which is a file object that represents "standard output stream"

If sys.stdout = Io is used. Stringio, the files entered into the Sys.stdout will be sent to Io.stringio. Available via IO. The Stringio.getvalue () function to get the value.

3 Command-line programming

Fileinput.input () iterates through all the rows redirected in the console. Fileinput.filename () and Fileinput.lineno (), the module can report the current file name and line number at any time.

Processing command-line option modules: Optparse and Getopt

  

Import Optparsedef Main ():    parser = Optparse. Optionparser ()    parser.add_option ("-W", "--maxwidth", dest = "MaxWidth", type = ' int ', help = ("The maximum number of Characters that can is "output to string Fields[default:%default]"))    parser.add_option ("-F", "--format", dest = ' Format ', help = "The format used for outputting numbers" "[Default:%default]")    parser.set_defaults (maxwidth = 100, Format = ". 0f") #设置初始值    Opts,args = Parser.parse_args ()

4 Mathematics and Numbers

Built in: Int,float,complex

Numeric standard library: Decimal. Decimal,fraction. Fraction

Standard Math function: Math

Complex mathematical functions: Cmath

Random Number: Randomness

5 Time and date

Calendar and DateTime

6 algorithms and combined data types

Bisect searching ordered sequences

HEAPQ converting a sequence to a heap

Import heapqheap = []heapq.heappush (Heap, (5, "Rest")) heapq.heapify () #将列表转换为堆, you can automatically complete the necessary reordering Heapq.heappop () # Removes the minimum item heapq.merge () #任意数量的排序后的迭代作为参数返回一个迭代子 from the heap.

The collection package offers Collectio.defaultdict and Collection.named-tuple, collection. UserList, collection. UserDict.Collection.deque (same as list) but list only adds or deletes at the end of the list with very fast speed, Collection at the beginning and end of the process. )

Collection. Orderdict and Collection.counter provide a quick and easy way to keep a variety of technologies. Returns a list of unique or most common elements in a two-tuple (element, number).

The array provides the sequence type: Array.array (the difference from the list is that the object type is fixed)

Weakref: Weak reference function. (The difference from the usual reference is that if there is only a weak reference to an object, the object can still be dispatched to garbage collection to prevent memory from being present only because of its reference.)

7 files, directories and process processing

The Shutil module provides a high-level function for file and directory processing, including the Shutil.copy () and Shutil.copytree () functions that copy files with the entire directory, and the Shutil.move () of the Mobile directory tree Move the Shutil.rmtree () function of the entire directory tree

Temporary file creation should use the Tempfile module, Tempfile.mkstemp () to create a temporary object.

filecmp for file comparison filecmp.cmp (), Whole directory comparison filecmp.cmpfiles ()

Processing and multiprocessing

OS module: Os.environ environment variable name and value. OS.GETCWD can get the program working directory. Os.chdir modify the working directory. Os.access determines whether a file exists or is readable or writable. Os.listdir () the list of entries in the given directory Os.stat () returns the file with various information items (mode, access time, size) of the directory Os.mkdir () Creating the Catalog Os.make-dirs () to create the intermediate directory. Os.rmdir () Remove empty directory os.rename () rename Os.walk () iterate over the entire directory tree, retrieving the name of each file and directory sequentially Os.path.abspath () returns the absolute path Os.path.split () returns a two-tuple, The first item contains the path the second item is the file name (Os.path.basename () and Os.path.dirname ()) file name can also be divided into two parts (name and extension) Os.splitext (). Os.path.join () accepts any number of path strings and returns using a specific delimiter.

Python Standard library Overview

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.