Python has a powerful standard library. The core of the Python language only contains common types and functions such as numbers, strings, lists, dictionaries, and files, while the Python standard library provides additional functions such as system management, network communication, text processing, database interfaces, graphics systems, and XML processing. .
The main functions of the Python standard library are:
- Text processing, including text formatting, regular expression matching, text difference calculation and merging, Unicode support, binary data processing and other functions
- File processing, including file operations, creating temporary files, file compression and archiving, operating configuration files, etc.
- Operating system functions, including thread and process support, IO reuse, date and time processing, calling system functions, logging, etc.
- Network communication, including network sockets, SSL encrypted communication, asynchronous network communication and other functions
- Network protocol, support HTTP, FTP, SMTP, POP, IMAP, NNTP, XMLRPC and other network protocols, and provide a framework for writing network servers
- W3C format support, including HTML, SGML, XML processing.
- Other functions, including internationalization support, mathematical operations, HASH, Tkinter, etc.
sys module
The sys module includes a set of very useful services, containing many functions and variables, used to process the configuration and resources of the Python runtime, so that it can interact with the system environment outside the previous program, such as the Python interpreter.
List of common functions of sys module:
os module
In automated testing, it is often necessary to find operating files, such as finding configuration files (thereby reading the information of the configuration files), finding test reports (thus sending test report emails), and often operating on a large number of files and a large number of paths. Depends on os module
In the Python interface, you can view the detailed introduction of the os module through the help() command, and press q to exit help()
import os
help(os)
os and its submodules contain many functions, here we introduce common submodules
(1) About the path submodule:
(2) About files/directories/attributes
(3) About environment variables
(4) About the process
math module
Digital processing module
random module
Generate random numbers
platform module
Get operating system details
time module
The time library provides various operation time values
strftime():
datetime module:
datetime.date() class:
datetime.datetime()类:
datetime.time()类:
JSON module
JSON is a lightweight data exchange format. Generally, the data returned by API is mostly JSON and XML. If JSON is returned, the acquired data will be converted into a dictionary for easy processing in the program.
There are two methods commonly used in the json library: dumps and loads():
Example:
1. Convert the dictionary to a JSON string
import json
dict = {
'user':[{
'user1': 123}, {
'user2': 456}]}
type(dict)
json_str = json.dumps(dict)
type(json_str)
2. Convert JSON string to dictionary
d = json.loads(json_str)
type(d)
Data type after JSON and Python decoding: