python modules common usage

1, time modules (※※※※)

import time #

Import time module

print(time.time()) #

Returns the current time stamp, it can be used to calculate the running time

print(time.localtime()) #

Returns the local time of structured time format parameter defaults to timestamp

print(time.gmtime) #

Back UTC time format structured time

print(time.mktime(time.localtime())) #

The time stamp of the conversion structure

print(time.strftime("%Y-%m-%d %X",time.localtime())) #

The conversion time is structured string time

print(time.strptime("2019:9:30:12:55:36","%Y:%m:%d:%X")) #

Converts a string structured time to time

print(time.asctime()) #

Or represents a structured time tuple conversion time to 'Sun Jun 20 23:21:05 1993' format

print(time.ctime()) #

The time stamp is converted to time.asctime format, default parameters time.time

PS: As usual diet, the strftime commonly used, and it is the strptime anti operation, the output format of the following methods can be more in line with people’s habits

import datetime #

Import module datetime

print( #

Output 2019-09-3015: 09.562809 the format: 15


2, random module (※※)

import random

print(random.random()) #

Returns a random floating point number between 0 and 1

print(random.randint(1,3)) #

Returns a random integer between 1 and 3, comprising 3

print(random.randrange(1,3)) #

Returns a random integer between 1 and 3, excluding 3

print(random.choice([1,'23',[4,5]])) #

Returns a list of random data

print(random.sample([1,'23',[4,5]],2)) #

Randomized return of two data in the list

print(random.uniform(1,3)) #

And the floating point between 1-3

item=[1,3,5,7,9] random.shuffle(item) print(item) #

The item list upset


3, os module (※※※※)

import os

os.getcwd() #

Returns the path to the current script working directory

os.chdir("dirname") #

Change the current working directory script

os.makedirs('dirname1/dirname2') #

Generate multi-layer recursive directories

os.removedirs('dirname1') #

If the directory is empty deleted, and recursively to the parent directory, if also continue to delete it is empty, and so on

os.mkdir('dirname') #

Generate single-level directory

os.rmdir('dirname') #

Delete single stage empty directory, if the directory is not empty and you can not delete an error

os.listdir('dirname') #

Return in a list of all files and subdirectories in the specified directory, including hidden files

os.remove() #

Delete a file

os.rename("oldname","newname") #

Rename the file or directory

os.stat('path/filename') #

Get file or directory

os.linesep #

Output current platform using line terminator (the win as "\ r \ n", as "\ n" under Linux)

os.pathsep #

Outputs the string used to split the file path (under win;, under Linux:)

os.path.abspath(path) #

Returns the absolute path normalized by path

os.path.split(path) #

Return path into the directory and file name tuple

os.path.dirname(path) #

Returns the directory path of

os.path.basename(path) #

The return path of the file name, if the path to / or \ end returns null

os.path.exists(path) #

path exists return True, there is no return False

os.path.isabs(path) #

path is the absolute path returns True

os.path.isfile(path) #

path is an existing file returns True, otherwise False

os.path.isdir(path) #

path is an existing directory returns True, otherwise False

os.path.join(path1[, path2[, ...]]) #

After combination of a plurality of paths returned, before the first parameter is ignored absolute path

os.path.getatime(path) #

Return path points to a file or directory last accessed time

os.path.getmtime(path) #

Return path points to a file or directory last modified

PS: wherein os.path.join more common.


4, sys module (※※※)

import sys

sys.argv #

Command line parameters List, the first element is the path to the program itself

sys.exit(n) #

Exit the program, when the normal exit exit (0)

sys.version #

Version Get Python interpreter

sys.maxint #

The maximum value of Int

sys.path #

Back module search path, using initialization value of the environment variable PYTHONPATH

sys.platform #

Returns the name of the operating system platform

PS: sys.argv which is more commonly used, the following code enables the printing progress bar, use the flush refresh the screen printing

import sys,time

for i in range(100):



import json
dic={'name':'alvin','age':23,'sex':'male'} #

The definition of a dictionary, a dictionary format


Serialized object

','w') j=json.dumps(dic) #

The sequence of the dictionary, i.e. single quotation marks becomes double, and then double and single primer or primer becomes all programming languages ​​are available on the outermost string format

f.write(j) #

Use can also be used dump, this is equivalent to two rows json.dump (dic, f)

f.close() f=open('

Serialized object

') data=json.loads( #

Deserialize the string, i.e., into the dictionary format string format, usage load may be used, this is equivalent to the two line data = json.load (f)

PS: dump and load usage more concise, but only file operations, so it is recommended dumps and loads. Further dumps and loads not necessarily be used in conjunction, as long as the format string dumps satisfied, loads can operate directly, dump, and load the same way. pickle and json substantially the same usage, json data can be exchanged between different languages, but only between the pickle python. json serialized only basic data types, and the pickle can serialize all data types, including classes, functions can be serialized (not used)


6, shelve module (※※※)

import shelve

def member_info(name, age):
    print("Member info:", name, age)

name = ['Jack', 'Maxwell', 'Tom']
info = {'name': 'Maxwell', 'age': 18}

with'demo') as data:
    data['name'] = name  #

Persistence list

data['info'] = info #

Persistence dictionary

data['func'] = member_info

PS: shelve module is simpler than pickle module, open only a function that returns the object dictionary-like, readable and writable. The key must be a string, and the value may be supported data types python


7, xml module (※※)

xml format, with <> difference data structures:


        "Austria" direction="E"/>
        "Switzerland" direction="W"/>
        "Malaysia" direction="N"/>
        "Costa Rica" direction="W"/>
        "Colombia" direction="E"/>

View Code

Xml python operation modules for:

import xml.etree.ElementTree as ET
tree = ET.parse("xmltest.xml")
root = tree.getroot()

---------- ---------- traverse xml document

for child in root: print(child.tag, child.attrib) for i in child: print(i.tag,i.text) #

---------- ---------- only traversal year node

for node in root.iter('year'): print(node.tag,node.text) #


for node in root.iter('year'): new_year = int(node.text) + 1 node.text = str(new_year) node.set("updated","yes") tree.write("xmltest.xml") #

Delete node ---------- ----------

for country in root.findall('country'): rank = int(country.find('rank').text) if rank > 50: root.remove(country) tree.write('output.xml')

View Code

How to create xml document:

import xml.etree.ElementTree as ET

new_xml = ET.Element("namelist")
name = ET.SubElement(new_xml,"name",attrib={"enrolled":"yes"})
age = ET.SubElement(name,"age",attrib={"checked":"no"})
sex = ET.SubElement(name,"sex")
sex.text = '33'
name2 = ET.SubElement(new_xml,"name",attrib={"enrolled":"no"})
age = ET.SubElement(name2,"age")
age.text = '19'
et = ET.ElementTree(new_xml) #

Generation document object

et.write("test.xml", encoding="utf-8",xml_declaration=True) ET.dump(new_xml) #

Print format generated

View Code

PS: json xml easier than with them, but xml appear very early, so that now a lot of system interface or xml


8, re module (※※※※※)

import re
ret=re.findall('','helloalvin') #

Yuan characters usage, a. Represents one character

print(ret) #

Output [ 'alvin'], re.findall is to retrieve all of the substring in string satisfies format and placed in the list

ret=re.findall('^a...n','alvinhelloawwwn') #

^ Metacharacter usage, indicates the start of matches from the beginning of the string

print(ret) #

Output [ 'alvin']

ret=re.findall('a...n$','alvinhelloawwwn') #

$ Metacharacter usage, indicates the start of matches from the end of the string

print(ret) #

Output [ 'awwwn']

ret=re.findall('abc*','abcccc') #

* Metacharacter usage, represents the character c repeated several times, but also for 0, will automatically match the most times, said the greedy match

print(ret) #

Output [ 'abcccc']

ret=re.findall('abc+','abccc') #

+ Metacharacter usage, represents the character c repeated many times, a minimum of 1, will automatically match the most times

print(ret) #['abccc'] ret=re.findall('abc?','abccc') #

Metacharacters? Usage, represents the character c Repeat 0 or 1, will automatically match 1

print(ret) #

Output [ 'abc']

ret=re.findall('abc{1,4}','abccc') #

Element {} is used characters, character c represents a range specified number of times, it will automatically match the most times

print(ret) #

Output [ 'abccc']

PS:?? Above metacharacters *, +, {} are all greedy matching, that is, by as much as possible the number of matches, plus behind the numbers can become inert match

ret=re.findall('abc*?','abcccccc') #

Inert match for the minimum number of characters c 0 times

print(ret) #

Output [ 'ab']

ret=re.findall('a[bc]d','acd') #

Meta character set character [] usage, denotes a selected focus matching character

print(ret) #

Output [ 'acd']

ret=re.findall('[a-z]','acd') #

Functional character set [] symbols: - ^ \, a-z represents all the letters from a to z

print(ret) #

Output [ 'a', 'c', 'd']

ret=re.findall('[.*+]','') #

In addition - ^ \ other symbols becomes general character, non-functional

print(ret) #

Output [ '.', '+']

ret=re.findall('[^ab]','45bdha3') #

Character Set [] ^ represents the "non", ^ ab a and b, i.e. the non-character

print(ret) #

Output [ '4', '5', 'd', 'h', '3']

ret=re.findall('[\d]','45bdha3') #

\ D character that is matched in digital format

print(ret) #

Output [ '4', '5', '3']

import re
=re.findall('c\l','abc\le') #

Metacharacters \ usage, can the general and special characters conversion, here \ l have a special meaning, it can not be retrieved in a string

print(ret) #

Output []

ret=re.findall('I\b','I am LIST') #

\ B matches represent a special character, such as space, &, #, etc.


\ D represents matches any decimal number corresponding to the class [0-9]


\ D represents any non-numeric characters match, equivalent to the class [^ 0-9]


\ S represents any whitespace character match, equivalent to the class [\ t \ n \ r \ f \ v]


\ S represents any non-blank character match, equivalent to the class [^ \ t \ n \ r \ f \ v]


\ W represents any alphanumeric character match, equivalent to the class [a-zA-Z0-9_]


\ W represents any non-alphanumeric characters match, corresponding to [a-zA-Z0-9_ ^] Class

print(ret) #

Output []

ret=re.findall(r'I\b','I am LIST') #

\ B have a special meaning, so the need to escape \\ b or r '\ b' again

print(ret) #

Output [ 'I']

ret=re.findall('c\\l','abc\le') print(ret) #

Output []

ret=re.findall('c\\\\l','abc\le') #

python interpreter escape and re escapes are different, so the need to pass more than \ to escape

print(ret) #

Output [ 'c \\ l']

ret=re.findall(r'c\\l','abc\le') #

You can also escape again directly preceded by r

print(ret) #

Output [ 'c \\ l']

m = re.findall(r'(ad)+', 'add') #

Metacharacters () usage, and () the contents as a whole to match

m = re.findall(r'(ad)+', 'adadad') #

The ab as a whole

print(m) #

Since the contents of the priority match () group, the priority output [ 'ad']

m1 = re.findall(r'(?:ad)+', 'adadad') #

To cancel the above-mentioned priority, we need to add?:

print(m1) #

Output [ 'ad', 'ad', 'ad']'(ab)|\d','rabhdg8sd') print( #

Output ab, function to find a match to the first substring matching a returned object may be () method to give the sub-group in the string through the string, if no match is returned None'(?P\d{2})/(?P\w{3})','23/com') #

Fixed format? P , id and name indicates the name of the packet, match-

print( #

Output 23 / com

print('id')) #

Output 23, group (group name) may acquire a value corresponding to the role of the packet is reusable several times, to improve efficiency

re module common method

import re

re.match('a','abc').group() # usage and the same, but can only be matched from the beginning of the string

ret=re.split('[ab]','abcd') #

Press the 'a' split, left to give '', to obtain the right 'BCD', then split b 'BCD', left to give '', to obtain the right 'CD'

print(ret) #

Output [ '', '', 'cd']

ret=re.sub('\d','abc','alvin5yuan6',1) #

1 represents a matching rule parameters or character parameters. 2 shows an alternative string, 3 represents the parameter string matching, matching parameter represents the number 4, do not fill all of the default alternative

print(ret) #

Output alvinabcyuan6

ret=re.subn('\d','abc','alvin5yuan6') #

And replacement times can be obtained after replacement string

print(ret) #

Output ( 'alvinabcyuanabc', 2)

obj=re.compile('\d{3}') #

First rule to an object'abc123eeee') #

Object calls modular approach

print( #

Output 123, the advantage of repeated calls, write rules to avoid duplication, improve efficiency

ret=re.finditer('\d','ds3sy4784a') #

Find content generated iterator

print(ret) #

Returns an iterator

print(next(ret).group()) #

Outputs '3', using the iterative output group

print(next(ret).group()) #

Output '4', the data of this large common iterative method does not account for memory


9, logging module (※※※※※)

import logging  

logging.debug('debug message') #

Log level grade CRITICAL> ERROR> WARNING> INFO> DEBUG> NOTSET'info message') logging.warning('warning message') logging.error('error message') logging.critical('critical message') #

Output WARNING: root: warning message

#  ERROR:root:error message #  CRITICAL:root:critical message #

The default log level is WARNING, and the back of the print itself, the format for the log level: Logger Name: User output messages

import logging  
                    format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',  
                    datefmt='%a, %d %b %Y %H:%M:%S',  
                    filemode='w') #

Use basicConfig format, level set the logging level; format specified display format; datefmt specified date and time format; filename specified log storage file name; the fileMode Open file filename specified, the default value "a"

logging.debug('debug message')'info message') logging.warning('warning message') logging.error('error message') logging.critical('critical message')#

Output cat /tmp/test.log

#  Sun, 13 Oct 2019 16:29:53[line:9] DEBUG debug message #  Sun, 13 Oct 2019 16:29:53[line:10] INFO info message #  Sun, 13 Oct 2019 16:29:53[line:11] WARNING warning message #  Sun, 13 Oct 2019 16:29:53[line:12] ERROR error message #  Sun, 13 Oct 2019 16:29:53[line:13] CRITICAL critical message

PS: The above format to format parameters may be used:

% (Name) s: Logger names% (levelno) s: digital form log level% (levelname) s: text log level% (pathname) s: call log output function of the full path name of the module may not % (filename) s: calling module log output function filename% (module) s: invoking module name log output function% (funcName) s: calling a function for a log output function of% (lineno) d: call log output row function code statement% (created) f: number of milliseconds% (asctime) s output log information created since Logger:: string represented by the current time UNIX standard floating point representing time% (relativeCreated) d current time, a default format “2019-10-1316: 49: 45,896” (comma milliseconds later)% (thread) d: thread ID may not% (threadName) s: thread name, may not% (process ) d: process Message output to the user: ID, may not% (message) s

About logger objects

import logging

logger = logging.getLogger() #

Function getLogger ([name]) Returns a logger object is returned if no name root logger

fh = logging.FileHandler('test.log') #

Create a handler, a log file for writing

ch = logging.StreamHandler() #

Then create a handler, for output to the console

formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') #

Formatter specified log display format

fh.setFormatter(formatter) ch.setFormatter(formatter) logger.addHandler(fh) logger.addHandler(ch) #

fh logger object can add a plurality of objects and ch

logger.debug('logger debug message')'logger info message') logger.warning('logger warning message') logger.error('logger error message') logger.critical('logger critical message') #

Output 2019-10-14 19: 51: 23,552 - root - WARNING - logger warning message

                    #  2019-10-14 19:51:23,552 - root - ERROR - logger error message                     #  2019-10-14 19:51:23,552 - root - CRITICAL - logger critical message

PS: Here can logger.setLevel (logging.Debug) Set the log level logger, the default log level is WARNIING, if additional logger create two objects, if the same parameter name and returns the Logger instance is the same, one of the back covering the front of the log level of the log level, the front and rear of the log will again reprinting


10, configparser module (※※※※)

Many software document format like this:

ServerAliveInterval = 45
Compression = yes
CompressionLevel = 9
ForwardX11 = yes
User = hg
Port = 50022
ForwardX11 = no

View Code

How it generates such a document with a python:

import configparser
config = configparser.ConfigParser()
config["DEFAULT"] = {'ServerAliveInterval': '45',
                      'Compression': 'yes',
                     'CompressionLevel': '9'} #

By adding data config, similar to the dictionary

config[''] = {} config['']['User'] = 'hg' config[''] = {} topsecret = config[''] topsecret['Host Port'] = '50022' topsecret['ForwardX11'] = 'no' config['DEFAULT']['ForwardX11'] = 'yes' with open('example.ini', 'w') as configfile: config.write(configfile)


import configparser

config = configparser.ConfigParser()'example.ini') #

Need to be read and then perform a lookup operation

print(config.sections()) #

In addition to the output of a first key, is [ '', '']

print('' in config) #

Output False

print(config['']['User']) #

Output hg

for key in config['']: print(key) #

Output Port

#ForwardX11 print(config.options('')) #

Output [ 'user'], i.e., key

print(config.items('')) #

Output [( 'user', 'hg')], i.e., the entire items

print(config.get('DEFAULT','compression')) #

Output yes


-------------------------------------------------- ------------- lookup operation

config.add_section('yuan') #

-------------------------------------------------- ------------- increased operating

config.remove_section('') config.remove_option('','user') #

-------------------------------------------------- ------------- delete operation

config.set('','k1','11111') #

-------------------------------------------------- ------------- modification operations

config.write(open('i.cfg', "w")) #

Function can directly open, not closed

View Code


11, hashlib module (※※)

import hashlib

m=hashlib.md5() #

Can also be used m hashlib.sha256 (), this module provides the main SHA1 =, SHA224, SHA256, SHA384, SHA512, MD5 algorithm

m.update('hello'.encode('utf8')) #

To hash a string of "hello"

print(m.hexdigest()) #

Output 5d41402abc4b2a76b9719d911017c592

m.update('alvin'.encode('utf8')) print(m.hexdigest()) #

Output 92a7e713c30abbb0319fa07da2a5c4af

m2=hashlib.md5() #

In addition the definition of a variable

m2.update('helloalvin'.encode('utf8')) #

The first two strings are concatenated together treatment

print(m2.hexdigest()) #

Output 92a7e713c30abbb0319fa07da2a5c4af, i.e. above m is the first hash hello, plus hash alvin

So now there is a large database, a lot of statistics commonly used string, then it might be the inverse solution, called the hit library. To avoid this, this can be the following methods:

import hashlib

m = hashlib.sha256('898oaFs09f'.encode('utf8')) #

Prior to treatment with a piece of a random string of hash

m.update('alvin'.encode('utf8')) print(m.hexdigest())

# Output


Of course there is a python module hmac, after it created internally and content key for us, and then to deal with encryption:

import hmac

h ='alvin'.encode('utf8'))
print (h.hexdigest()) #

Output 320df9832eab4c038b6c1d7ed73a5940


Leave a Reply