常用模块

常用模块

1 时间模块

博客

1.1 时间分为三种格式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import time
# 1、时间戳
>>>print(time.time())
1617282446.782401

# 2、格式化的字符
>>>print(time.strftime('%Y-%m-%d %H:%M:%S %p'))
2018-04-01 21:08:03 PM

# 3、结构化的时间对象
>>>obj = time.localtime()
>>>print(obj)
time.struct_time(tm_year=2021, tm_mon=4, tm_mday=1, tm_hour=21, tm_min=8, tm_sec=31, tm_wday=3, tm_yday=91, tm_isdst=0)
>>> print(obj.tm_year)
2021
>>> print(obj.tm_wday)
3

1.2 三种格式转换

avatar

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#转换练习
#1.localtime 和 gmtime
>>> print(time.localtime())
time.struct_time(tm_year=2021, tm_mon=4, tm_mday=1, tm_hour=21, tm_min=14, tm_sec=15, tm_wday=3, tm_yday=91, tm_isdst=0)
>>> print(time.gmtime())
time.struct_time(tm_year=2021, tm_mon=4, tm_mday=1, tm_hour=13, tm_min=14, tm_sec=38, tm_wday=3, tm_yday=91, tm_isdst=0)
>>>

#2.struct_time转格式化的字符串时间
import time
struct_time = time.localtime(333333333)
strf_time = time.strftime("%m:%Y",struct_time)
>>>print(strf_time)
07:1980
#3.struct_time转时间戳
struct_time = time.strptime('2017-11-11 11:11:11','%Y-%m-%d %H:%M:%S')
res = time.mktime(struct_time)
>>>print(res)
1510369871.0

#4.asctime和ctime
>>>print(time.asctime())
Thu Apr 1 21:24:38 2021
>>>print(time.ctime())
Thu Apr 1 21:24:38 2021

#5.datetime
import datetime

print(datetime.datetime.now())
>>>2018-04-01 21:27:21.895063
print(datetime.datetime.now() + datetime.timedelta(hours=3))
>>>2018-04-02 00:27:55.533965
print(datetime.datetime.now() + datetime.timedelta(hours=-3))
>>>2018-04-01 18:28:35.094804
print(datetime.datetime.now() - datetime.timedelta(hours=3))
>>>2018-04-02 00:28:56.115935

res = datetime.datetime.now()
>>>print(res.date())
2018-04-01
>>>print(res.strftime('%H:%M:%S'))
21:29:55
>>>print(res.replace(hour=10))
2018-04-01 10:29:55.066139

2 random模块

2.1 基本用法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import random
#1.生成小数
>>> print(random.random()) # (0,1)
0.35029681919165045
#2.生成数字
>>> print(random.randint(3,5)) # [3,5]
5
#3.生成数字
>>> print(random.randrange(3,5)) # [3,5)
4
#4.从可迭代对象中随机选择一个
>>> print(random.choice([111,222,"aaa",(1,2,3)]))
(1, 2, 3)
#5.返回一个包含列表中任何两项的列表
>>> print(random.sample([111,222,"aaa",(1,2,3)],2))
[111, 222]
#6.返回小数
>>> print(random.uniform(1,3))
1.1889052508854532
>>> item = [1, 3, 5, 7, 9]
#7.shuffle() 方法将序列的所有元素随机排序。
>>> random.shuffle(item)
>>> print(item)
[5, 7, 9, 3, 1]

# 储备知识

2.2 asc字符编码表转换

avatar

1
2
3
4
print(chr(65))
print(chr(90))
print(ord('A'))
print(ord('Z'))

2.3 编写随机验证码

1
2
3
4
5
6
7
8
9
def make_code(size=6):
res = ''
for i in range(size):
num = str(random.randint(0, 9))
alpha = chr(random.randint(65, 90))
res += random.choice([num, alpha])
return res

print(make_code())

3 re模块

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import re

# res = re.findall('l','hello world 123')
# hello world 123
# l
# ['l','l','l']
# print(res)

# 正则表达式
# print(re.findall('\w',"hello w_o-+*rld 123"))
# hello w_o-+*rld 123
# \w
# print(re.findall('\W',"hello w_o-+*rld 123"))
# print(re.findall('\s',"hell\no w_o\t-+*rld 123"))
# print(re.findall('\S',"hell\no w_o\t-+*rld 123"))
# print(re.findall('\d',"hell\no w_o\t-+*rld 123"))
# print(re.findall('\D',"hell\no w_o\t-+*rld 123"))

# print(re.findall("^egon","heegonllo egon xxx egon yyy egon"))
# heegonllo egon xxx egon yyy egon
# ^egon
# print(re.findall("^egon","egonllo egon xxx egon yyy egon"))
# print(re.findall("egon$","egonllo egon xxx egon yyy egon1"))
# egonllo egon xxx egon yyy egon1
# egon$

# print(re.findall("^a","""abc
# a123
# a456
# 666
# """,re.MULTILINE))

# print(re.findall("\d\d$","""abc
# a123
# a456
# 666
# """,re.MULTILINE))

# print(re.findall("\d$","""abc
# a123
# a456
# 666
# """,re.MULTILINE))


# print(re.findall("\n","a\nb\tc\n"))
# print(re.findall("\t","a\nb\tc\n"))

# .: 代表匹配一个字符,该字符可以是除了换行符之外的任意字符
# print(re.findall("a.c","a1c a+c abc aAc a c aaaaac a\nc a\tc"))
# a1c a+c abc aAc a c aaaaac a\nc a\tc
# a.c
# ['a1c','a+c','abc','aAc','a c','aac','a\tc']

# print(re.findall("a.c","a1c a+c abc aAc a c aaaaac a\nc a\tc",re.DOTALL))


# []: 代表匹配一个字符,该字符可以是指定的字符
# print(re.findall("a[1A]c","a1c a+c abc aAc a c aaaaac a\nc a\tc"))
# print(re.findall("a[0-9]c","a1c a+c abc aAc a c aaaaac a\nc a\tc"))
# print(re.findall("a[a-zA-Z]c","a1c a+c abc aAc a c aaaaac a\nc a\tc"))
# print(re.findall("a[+*\-]c","a1c a+c abc a-c a*c a\c aAc a c aaaaac a\nc a\tc"))

# print(re.findall(r"a[+*\\-]c","a1c a+c abc a-c a*c a\c aAc a c aaaaac a\nc a\tc"))
# print(re.findall(r"a[-+*\\]c","a1c a+c abc a-c a*c a\c aAc a c aaaaac a\nc a\tc"))
# print(re.findall(r"a[^-+*\\]c","a1c a+c abc a-c a*c a\c aAc a c aaaaac a\nc a\tc"))


# *:左边那一个符号出现0次到无穷次
# ?:左边那一个符号出现0次到1次
# +:左边那一个符号出现1次到无穷次
# {n,m}:左边那一个符号出现n次到m次

# print(re.findall("ab*",'a ab abbb abbbb bbbba'))
# a ab abbb abbbb bbbba
# ab*
# ['a','ab','abbb','abbbb','a']

# print(re.findall("ab*",'a ab abbb abbbb bbbba'))

# print(re.findall("ab?","a ab abbb abbbb bbbba"))
# a ab abbb abbbb bbbba
# ab?
# ['a','ab','ab','ab','a']


# a ab abbb abbbb bbbba
# ab+
# ['ab','abbb','abbbb']
# print(re.findall("ab+","a ab abbb abbbb bbbba"))

# print(re.findall("ab{0,1}","a ab abbb abbbb bbbba"))
# print(re.findall("ab{0,}","a ab abbb abbbb bbbba"))
# print(re.findall("ab{1,}","a ab abbb abbbb bbbba"))
# print(re.findall("ab{3,5}","a ab abbb abbbb bbbba"))
# a ab abbb abbbb bbbba
# ab{3,5}

# print(re.findall("compan(?:ies|y)","Too many companies have gone bankrupt, and the next one is your company"))
# Too many companies have gone bankrupt, and the next one is your company
# compan(ies|y)

# .*
# print(re.findall('a.*c','a1231231+-c666666666c11111111'))
# print(re.findall('a.*?c','a1231231+-c666666666c11111111'))
# a1231231+-c666666666c11111111
# a.*c


print(re.findall("href='(.*?)'","<a href='http://www.baidu.com'>我特么是百度</a><a href='http://www.sina.com.cn'></a>"))
# <a href='http://www.baidu.com'>我特么是百度</a><a href='http://www.sina.com.cn'></a>
# href='.*?'

# ["href='http://www.baidu.com'","href='http://www.sina.com.cn'"]

4 logging模块

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
mport logging.config
import settings

def get_logger(name):
logging.config.dictConfig(settings.LOGGING_DIC)

# logger = logging.getLogger("用户相关")
# logger = logging.getLogger("交易相关")
logger = logging.getLogger(name)
return logger

logger = get_logger("交易相关")

# logger.debug("这是一条debug日志")
logger.info("egon给刘老师转了一个亿")
# logger.warning("这是一条warn日志")
# logger.error("这是一条error日志")
# logger.critical("这是一条critical日志")



###settins.py内容
"""
logging配置
"""

# 1、定义三种日志输出格式,日志中可能用到的格式化串如下
# %(name)s Logger的名字
# %(levelno)s 数字形式的日志级别
# %(levelname)s 文本形式的日志级别
# %(pathname)s 调用日志输出函数的模块的完整路径名,可能没有
# %(filename)s 调用日志输出函数的模块的文件名
# %(module)s 调用日志输出函数的模块名
# %(funcName)s 调用日志输出函数的函数名
# %(lineno)d 调用日志输出函数的语句所在的代码行
# %(created)f 当前时间,用UNIX标准的表示时间的浮 点数表示
# %(relativeCreated)d 输出日志信息时的,自Logger创建以 来的毫秒数
# %(asctime)s 字符串形式的当前时间。默认格式是 “2003-07-08 16:49:45,896”。逗号后面的是毫秒
# %(thread)d 线程ID。可能没有
# %(threadName)s 线程名。可能没有
# %(process)d 进程ID。可能没有
# %(message)s用户输出的消息

LOG1_PATH = 'a1.log'
LOG2_PATH = 'a2.log'

# 2、强调:其中的%(name)s为getlogger时指定的名字
fm1 = '%(asctime)s - %(filename)s:%(lineno)d - %(name)s - %(levelname)s: %(message)s'
fm2 = "%(asctime)s : %(message)s"

# 3、日志配置字典
LOGGING_DIC = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'formatter1': {
'format': fm1
},
'formatter2': {
'format': fm2
},
},
'filters': {},
'handlers': {
#打印到终端的日志
'console': {
'level': 'DEBUG',
'class': 'logging.StreamHandler', # 打印到屏幕
'formatter': 'formatter2'
},
#打印到文件的日志,收集info及以上的日志
'fh1': {
'level': 'DEBUG',
'class': 'logging.FileHandler', # 保存到文件
'formatter': 'formatter1',
'filename': LOG1_PATH,
'encoding': 'utf-8',
},
'fh2': {
'level': 'DEBUG',
'class': 'logging.FileHandler', # 保存到文件
'formatter': 'formatter1',
'filename': LOG2_PATH,
'encoding': 'utf-8',
},
},
'loggers': {
'': {
'handlers': ['console','fh1','fh2'],
'level': 'DEBUG',
'propagate': False,
},
'用户相关': {
'handlers': ['console','fh1'],
'level': 'DEBUG',
'propagate': False,
},
# '交易相关': {
# 'handlers': ['console','fh1','fh2'],
# 'level': 'DEBUG',
# 'propagate': False,
# },
},
}

5 序列化模块

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# 1、序列化
# dic = {'name':"egon","age":18}
#
# str_dic = str(dic)
# with open('a.txt',mode='wt',encoding='utf-8') as f:
# f.write(str_dic)

# 2、反序列化
# with open('a.txt',mode='rt',encoding='utf-8') as f:
# dic = eval(f.read()) # "{'name': 'egon', 'age': 18}"
# print(dic['name'])

# "print(111111)"
# eval("print(111111)")
# l = eval("[11,22,33]")
# print(l[0])


# 序列化的两大用途:
# 1、存档(pickle)
import pickle
import json

dic = {'name': "egon", "age": 18.5, "k1": True, 'k2': None, 'k3': (1, 2, 3), 'k4': {1, 2, 3}}
# json.dumps(dic) # 报错
# dic_pkl = pickle.dumps(dic)
# print(dic_pkl,type(dic_pkl))
# with open('a.pkl',mode='wb') as f:
# f.write(dic_pkl)

# with open('a.pkl',mode='wb') as f:
# pickle.dump(dic,f)

# with open('a.pkl', mode='rb') as f:
# 方式一
# res = f.read()
# dic = pickle.loads(res)
# print(dic)

# 方式二
# res = pickle.load(f)
# print(res)

# 2、跨平台交互数据(需要使用一种通用的数据格式->json)

import json

# ====================json 格式的序列化与反序列化1
# dic = {'name':"egon","age":18.5,"k1":True,'k2':None,'k3':(1,2,3)}
# json_str = json.dumps(dic)
# print(json_str,type(json_str))
# with open('a.json',mode='wt',encoding='utf-8') as f:
# f.write(json_str)

# with open('a.json',mode='rt',encoding='utf-8') as f:
# json_str = f.read()
# res = json.loads(json_str)
# print(res,type(res))

# ====================json 格式的序列化与反序列化2
# dic = {'name':"egon","age":18.5,"k1":True,'k2':None,'k3':(1,2,3)}
# with open('a.json',mode='wt',encoding='utf-8') as f:
# json.dump(dic,f)

# with open('a.json',mode='rt',encoding='utf-8') as f:
# res = json.load(f)
# print(res,type(res))


# 猴子补丁
import json
import ujson # pip3 install ujson

def monkey_patch():
json.dumps = ujson.dumps
json.loads = ujson.loads

monkey_patch()


json.dumps
json.loads

6 os模块

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import os

# os.remove()
# os.rename()

# print(os.getcwd())

# print(os.listdir(r"D:\python17期\day17\代码"))
# print(os.listdir(r"D:\python17期\day17\代码\aaa"))

# os.chdir(r"D:\python17期\day17\代码\aaa")
# print(os.getcwd())
# print(os.listdir("."))
# print(os.listdir(".."))
# print(os.listdir("../.."))

# print(os.curdir)
# print(os.pardir)

# os.makedirs('a/b/c/d/e')
# os.removedirs('a/b/c/d/e')
# os.rmdir('a/b/c/d/e')

# obj = os.stat('今日内容.txt')
# print(obj.st_size)

# print(os.path.getsize('今日内容.txt'))

# r"a\b\new.txt"


# with open('a.txt',mode='rb') as f:
# print(f.read())


# with open('a.txt',mode='rt',encoding='utf-8') as f:
# print(f.readlines())

# res = os.system("tasasdfasklist")
# print('='*10)
# print(res)


# print(os.environ)

# print(os.environ['USERNAME'])

# os.environ['current_user'] = 'egon'
# print(os.environ)
# print(os.environ['current_user'])
# print(os.environ.get('current_user'))



# print(os.path.abspath('a/b/c.txt'))

# res = os.path.split(r"D:\a\b\new.txt")
# print(res)
# print(os.path.dirname(r'D:\a\b\new.txt'))
# print(os.path.basename(r'D:\a\b\new.txt'))

# print(os.path.exists(r'D:\python17期\day17\代码\aaa'))
# print(os.path.isfile(r'D:\python17期\day17\代码\aaa'))
# print(os.path.isdir(r'D:\python17期\day17\代码\aaa'))


# print(os.path.isabs(r"D:\a\b\c.txt"))
# print(os.path.isabs(r"/a/b/c.txt"))
# print(os.path.isabs(r"a/b/c.txt"))
# print(os.path.join("D:\\",'a','b','c.txt'))


# print(os.path.normcase('c:/WiNdows\\system32\\') )


# print(os.path.normpath(r"C:\a\b\..\.."))


print(__file__)
print(os.path.dirname(os.path.dirname(__file__)))


print(os.path.normpath(os.path.join(__file__,"..",'..')))

7 sys模块

1
2
3
4
5
6
7
8
import sys

# sys.path

# print(sys.argv) # 用于从命令行接收参数

src_file = sys.argv[1]
dst_file = sys.argv[2]

8 shutil模块

1
2
3
4
5
6
7
8
9
10
import shutil

# ret = shutil.make_archive("data_bak", 'gztar', root_dir=r'D:\python17期\day17\代码\aaa') # gzip,data_bak.tar.gz


# import tarfile
#
# t = tarfile.open("data_bak.tar.gz",'r')
# t.extractall('bbb/')
# t.close()

9 xml和shelve模块

1
2
3
4
5
6
7
8
import shelve

f = shelve.open(r'a.shl')
# f['xxx']={'k1':(1,2,3),'k2':{4,5,6}}
# f['yyy']=[111,222,333]
print(f['xxx']['k2'])
print(f['yyy'])
f.close()

10 subprocess模块

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import subprocess

# linux系统 & mac系统:ps aux
# windows系统:tasklist
obj = subprocess.Popen('tasklist',shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)

# import time
# time.sleep(3)

# print(obj)
res1 = obj.stdout.read()
res2 = obj.stderr.read()
print(res1.decode('gbk'))
print(res2)

11 hashlib模块

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import subprocess

# linux系统 & mac系统:ps aux
# windows系统:tasklist
obj = subprocess.Popen('tasklist',shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)

# import time
# time.sleep(3)

# print(obj)
res1 = obj.stdout.read()
res2 = obj.stderr.read()
print(res1.decode('gbk'))
print(res2)