Python之路【第四篇】:Python基础4之常用模块
一、re模块
#!/usr/bin/env python3
import re
# 从头开始匹配
str1 = 'alex1rain2jack3helen rachel8'
res = re.match('alex',str1)
print(res.group())
# 匹配整个字符串,直到找到一个匹配
str2 = 'alex1rain2jack3helen rachel8'
res2 = re.search('rain',str2) # 返回rain
print(res2.group())
res3 = re.match('rain',str2) # 返回None
if res3 != None:
print(res3.group())
# 找到所有要匹配的字符并返回列表格式
str3 = 'alex1rain2jack3helen rachel8'
res4 = re.findall('[0-9]+',str3)
print(res4) # ['1', '2', '3', '8']
# 将匹配到的格式当做分割点对字符串分割成列表
str4 = 'alex1rain2jack3helen rachel8'
res5 = re.split('[0-9]+',str4) # ['alex', 'rain', 'jack', 'helen rachel', '']
print(res5)
# 替换匹配到的字符,返回替换后的字符串
str5 = 'alex1rain2jack3helen rachel8'
res6 = re.sub('[0-9]+','-',str5)
print(res6) # alex-rain-jack-helen rachel-
# 匹配手机号码
phone_str1 = "hey my name is alex, and my phone number is 13651054607, please call me if you are pretty!"
phone_str2 = "hey my name is alex, and my phone number is 18651054604, please call me if you are pretty!"
res = re.search('1[358]\d{9}',phone_str2)
print(res.group())
# 匹配IP V4
ip_addr = "inet 192.168.60.26 netmask 0xffffff00 broadcast 192.168.60.255"
res = re.search('\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}',ip_addr)
print(res.group())
# 匹配email
email = "alex.li@126.com http://www.oldboyedu.com"
res = re.search('[0-9.a-z]{0,26}@[0-9.a-z]{0,20}.[0-9a-z]{0,8}', email)
print(res.group())
二、subprocess模块
import subprocess
# subprocess开启新的进程去执行系统命令
# 通过原生的shell取执行系统命令
subprocess.run('df -h',shell=True)
'''
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 19G 1.9G 16G 11% /
tmpfs 495M 0 495M 0% /dev/shm
/dev/sda1 190M 27M 153M 15% /boot
CompletedProcess(args='df -h', returncode=0)
'''
# 通过subprocess模块包装好的方式去执行系统命令
subprocess.run(['df','-h'])
'''
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 19G 1.9G 16G 11% /
tmpfs 495M 0 495M 0% /dev/shm
/dev/sda1 190M 27M 153M 15% /boot
CompletedProcess(args=['df', '-h'], returncode=0)
'''
# 获取执行系统命令后的返回结果
res = subprocess.Popen('df -h',shell=True,stdout=subprocess.PIPE)
res.stdout.read()
# 需要交互的命令示例
obj = subprocess.Popen('python', shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
obj.stdin.write(b'print 1 \n ')
obj.stdin.write(b'print 2 \n ')
obj.stdin.write(b'print 3 \n ')
obj.stdin.write(b'print 4 \n ')
out_error_list = obj.communicate(timeout=10)
print(out_error_list)
三、logger 模块
import logging
def logger(log_type,log_path):
# create logger
logger = logging.getLogger(log_type)
logger.setLevel(logging.DEBUG)
# create formatter
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
# create console handler and set level to debug and set formatter
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.DEBUG)
console_handler.setFormatter(formatter)
# create file handler and set level to warning and set formatter
file_handler = logging.FileHandler(log_path)
file_handler.setLevel(logging.WARNING)
file_handler.setFormatter(formatter)
# add console_handler and file_handler to logger
logger.addHandler(console_handler)
logger.addHandler(file_handler)
# return logger
return logger
if __name__ == '__main__':
access_logger = logger('access','./conf/accces.log')
access_logger.debug('debug message')
access_logger.info('info message')
access_logger.warning('warning message')
access_logger.error('error message')
access_logger.critical('critical message')
trans_logger = logger('transaction','./conf/transaction.log')
trans_logger.debug('debug message')
trans_logger.info('info message')
trans_logger.warning('warning message')
trans_logger.error('error message')
trans_logger.critical('critical message')
浙公网安备 33010602011771号