大鹏

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

【python自动化第五篇:模块】

1
今天内容:
  1. 模块的定义
  2. 导入方法
  3. import的本质
  4. 导入优化
  5. 模块分类
  6. 模块介绍
  7. configparser模块
  8. hashlib模块
  9. Re模块

一、模块定义:

    用来在逻辑上组织python代码(变量,函数,逻辑,类);本质就是为了实现一个功能(就是以.py结尾的python文件)

    比如说:文件名为test.py的文件,他的模块名就是test

例如:在同一个文件夹下创建main.py(导入模块的程序),test.py(模块文件)

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#main.py文件内容如下:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
 
import test
printtest.name)                              #导入test模块下的变量
print(test.say_hello())                    #导入test模块下的函数say_hello()##要注意调用函数要加括号
 
 
 
#test.py文件内容如下:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
 
name = "wanghui"
def say_hello():
    print('hello %s'%name)

  

二、导入方法:

  import module_name         (单个导入)

  import module1,module2,module3.....           (多个导入用英文逗号隔开)

  from module_name import *                         (导入所有的module_name下的所有代码拿到当前的程序下先执行一遍,建议不要这样使用)

  from module_name import logger as logger_module_name               (导入module_name模块下的logger函数,并重命名为logger_module_name使用的时候用logger_module_name函数)

三、import的本质

  import导入:相当于是将模块的内容解释之后赋值给需要导入该模块的程序,调用的时候需要以module.func(模块对应的方法)来调用这个模块的方法

  from module import func这样的导入:相当于是打开module这个文件并找到遁影的func方法执行一遍,直接用func名来调用即可。

示例如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#main.py文件内容如下:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
 
import test
print(test.name)                              #导入test模块下的变量
print(test.say_hello())                       #导入test模块下的函数say_hello()##要注意调用函数要加括号,还有要将模块名写上,否则出错
#from导入
from module1 import name,logger               #from导入test模块下的name,logger方法,调用时只需调用这个name和logger方法即可
print(name)
logger()
 
 
 
#test.py文件内容如下:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
 
name = "wanghui"
def say_hello():
    print('hello %s'%name)
def logger():
    print("in the test!!!")

  包的定义:本质就是一个目录(必须带有一个__init.py__的文件),

    用法:从逻辑上组织模块的

  包的导入:导入包的目的就是为了解释包下的init.py文件

  路径搜索的意义:就是为了能在import的时候找到正确的模块路径(也就是跨目录的模块搜索功能),方便找到模块予以执行;否则将在自己所在的文件目录下查找模块,找的到的话 就能够使用模块,找不到的话就报错。

  路径搜索用到的模块:sys,os

1
2
3
4
5
6
7
8
9
import sys,os
print(os.path.abspath(__file__))   #获取当前文件的路径绝对路径(结果D:\project\s14\day5\review\main.py)
#n那么这会儿要是往上走一级目录的话就是要os.path.dirname(),线来获取此文件所在的目录
print(os.path.dirname(os.path.abspath(__file__)))   #这会儿就到文件所在的目录了(结果D:\project\s14\day5\review)
#因为你的module1.py模块在day5下呢,所以现在还是找不到的,那我们就再上一层吧,还是用os.path.dirname()结合上一个os.path.dirname(os.path.abspath(__file__))就可以实现往上一级目录定位了。
print(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) #这样子就可以找到day5这个目录了
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))   #将这个路径加载到当前程序的环境变量中,以便调用模块的时候能够找的到模块(当然也可以使用insert来添加到指定位置)
import module1
print(module1.name)

 这样的话就可以实现跨目录的调用了,一定要记得加载到环境变量中,不然还是找不到呢。

 完整的导入技巧如下: 

1
2
3
4
5
6
#import 跨路径导入模块
import sys,os
BASE_DIR=os.path.dirname(os.path.abspath(__file__))  #定义当前文件完整路径
MODULE_DIR=os.path.dirname(os.path.dirname(os.path.abspath(__file__)))  #可已通过os.path.dirname()往上一级目录,
sys.path.append(MODULE_DIR)   #将当前模块路径写入当前调用程序的环境变量
import module_name                    #现在就可以导入跨路径的模块了       绝对路径<br>from day5 import package_name

四、导入优化

  像是有些模块的重复使用对的情况下,import的导入方法要弱于from module import func;毕竟重复使用的时候也只是使用模块对应的方法。要单纯使用import的时候多个函数的模块调用会导致运行效率不高。故引入from modue import func 来直接定义好要调用的func,从而提高多多个函数的模块调用的效率!

五、模块的分类:

  1. 标准库(内置模块)
  2. 第三方库(开源模块)
  3. 自定义模块

六、标准库

  1.时间模块

    时间格式:时间戳,格式化的时间,元组话表示

          time: 

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
 
import time
t = time.time()          #时间戳格式显示,单位是秒
x = t          #遮掩算出来的就是从1970.1.1-00:00:00到现在经历的年
print(x)
 
q = time.localtime()     #时间元组格式的时间
print(q)                 #time.struct_time(tm_year=2016, tm_mon=8, tm_mday=22, tm_hour=2, tm_min=57, tm_sec=59, tm_wday=0, tm_yday=235, tm_isdst=0)
 
w = time.timezone        #格里尼治时间和本地时间的差(按秒计算)
print(w/3600)            #打印时区差
 
e = time.altzone         #夏令时(DST)和UTC时间差    time.daylight 是否使用夏令时
print(e/3600)            #打印小时差
 
#time.sleep(10)          #休息10秒
 
r = time.gmtime()        #需要传入秒数,然后就转换成时间元组(time.timezone),
print(r)                 #不加时间戳参数则表示打印的是格林尼治时间元组
 
y = time.localtime()     #转换成当地时间元组形式
print(y)                 #加参数则能够随着本地的时间格式转换
d = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())        #将元组结构时间转换成格式化时间
print(d)
 
f = time.strptime('2016-08-23 00:30:32',"%Y-%m-%d %H:%M:%S")   #格式化时间转换成元组结构时间
print(f)
 
g = time.asctime()                              #将时间转换成Tue Aug 23 00:35:52 2016格式
print(g)
 
k = time.ctime()                                #将时间戳格式转换成Tue Aug 23 00:35:52 2016格式
print(k)

  操作图如下:

  timedate模块:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import datetime
print(datetime.date)
print(datetime.time)
print(datetime.datetime.now())                #获取当前时间
print(datetime.datetime.now()+datetime.timedelta(3))    #获取到的三天后的时间
print(datetime.datetime.now()+datetime.timedelta(-3))   #获取三天前的时间
print(datetime.datetime.now()+datetime.timedelta(hours=3))   #获取三小时以后的时间
print(datetime.datetime.now()+datetime.timedelta(hours=-3))  #获取三小时钱的时间
print(datetime.datetime.now()+datetime.timedelta(minutes=3)) #获取三分钟以后的时间
print(datetime.datetime.now()+datetime.timedelta(minutes=-3)) #获取三分钟之前的时间
 
#时间替换
c_time = datetime.datetime.now()                       #获取当前时间
print(c_time.replace(minute=4,hour=2))                 #replace当前时间

  2.random

   random基础操作:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#!/usr/bin/env python
# -*- coding:utf-8 -*-
 
import random
 
print(random.random())      #打印生成的0到1之间的随机额浮点数
print(random.uniform(1,9))  #打印1到9之间的随机浮点数
print(random.randint(1,9))  #打印1到9之间的随机整数
print(random.randrange(1,9)) #打印1到9之间除了9之外的随机整数(顾头不顾尾,类似于range(9))
# #random.choie()         其中可以传入任意的数据类型并随机选其一
print(random.choice("abcdefghijklmn"))     #随机选择一个字符串
print(random.choice((1,2,3,[4,5,6],7,8)))      #随机选择元组中的一个参数(序列都能这么玩)
#print(random.choice({'name':"wanghui",'age':"24",'job':"IT"}))           #金测试字典中的元素不能被随机打印(因为时间支队,不能被拆散)
 
print(random.sample("hello",2))       #sample()对于定义的字符春后面的数字代表能够随机取出的个数(以列表的方式打印)
 
#洗牌效果:实现对其中的列表或者元组元素顺序的打乱
li_old = [1,2,3,4,5,6]
li_new = random.shuffle(li_old)     #洗牌操作
print(li_old)               #将li_old列表的元素乱序排列

  random实战生成六位验证码:

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
check_code = []                           #定义空列表,用于传递验证码用
for i in range(6):                       #定义验证码位数
    r = random.randint(0,5)               #定义生成验证码的次数
    if i == 2 or r == 4:                 #第二位或者第四次生成数字。将其加入空列表
        num = random.randint(0,9)
        check_code.append(str(num))
    else:                                #其他情况
        word = random.randint(65,90)     #定义字母并见此追加到列表中
        word_str = chr(word)
        check_code.append(str(word_str))
final_code = "".join(check_code)        #将最后生成的列表中的字符串去掉标点符号来join到一起
print(final_code)
 
#方法二:
import random
checkcode = ''
for m in range(6):                     #定义验证码位数
    current = random.randint(0,9)       #让数字活动起来
    if m ==2 or current ==4:           #位数和次数    (完全随机的话current == m)
        tmp = random.randint(0,9)       #如果次数随机等于m,则将其任意值赋给tmp
    else:
        tmp = chr(random.randint(65,90))   #其余的为字母
    checkcode+=str(tmp)                    #拼接字符串
print(checkcode)                           #打印验证码

  3.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
os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径
os.chdir("dirname")  改变当前脚本工作目录;相当于shell下cd
os.curdir  返回当前目录: ('.')
os.pardir  获取当前目录的父目录字符串名:('..')
os.makedirs('dirname1/dirname2')    可生成多层递归目录
os.removedirs('dirname1')    若目录为空,则删除,并递归到上一级目录,如若也为空,则删除,依此类推
os.mkdir('dirname')    生成单级目录;相当于shell中mkdir dirname
os.rmdir('dirname')    删除单级空目录,若目录不为空则无法删除,报错;相当于shell中rmdir dirname
os.listdir('dirname')    列出指定目录下的所有文件和子目录,包括隐藏文件,并以列表方式打印
os.remove()  删除一个文件
os.rename("oldname","newname")  重命名文件/目录
os.stat('path/filename')  获取文件/目录信息
os.sep    输出操作系统特定的路径分隔符,win下为"\\",Linux下为"/"
os.linesep    输出当前平台使用的行终止符,win下为"\t\n",Linux下为"\n"
os.pathsep    输出用于分割文件路径的字符串
os.name    输出字符串指示当前使用平台。win->'nt'; Linux->'posix'
os.system("bash command")  运行shell命令,直接显示
os.environ  获取系统环境变量
os.path.abspath(path)  返回path规范化的绝对路径
os.path.split(path)  将path分割成目录和文件名二元组返回
os.path.dirname(path)  返回path的目录。其实就是os.path.split(path)的第一个元素
os.path.basename(path)  返回path最后的文件名。如何path以/或\结尾,那么就会返回空值。即os.path.split(path)的第二个元素
os.path.exists(path)  如果path存在,返回True;如果path不存在,返回False
os.path.isabs(path)  如果path是绝对路径,返回True
os.path.isfile(path)  如果path是一个存在的文件,返回True。否则返回False
os.path.isdir(path)  如果path是一个存在的目录,则返回True。否则返回False
os.path.join(path1[, path2[, ...]])  将多个路径组合后返回,第一个绝对路径之前的参数将被忽略
os.path.getatime(path)  返回path所指向的文件或者目录的最后存取时间
os.path.getmtime(path)  返回path所指向的文件或者目录的最后修改时间

  3.sys模块:

1
2
3
4
5
6
7
8
9
10
#!/usr/bin/env python
# -*- coding:utf-8 -*-
#sys模块
 
import sys
print(sys.agv[1])   #窜入第一个参数
sys.exit(7)         #按照状态码为7退出
sys.maxint          #最大的int值
print(sys.platform) #打印操作系统平台
sys.stdout.write('please')   #系统输出,用于打印进度条

  4.phutil:高级的 文件、文件夹、压缩包 处理模块

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
#shutil模块:进行文件的copy,还能打包
import shutil
f_old = open('test1.txt',encoding='utf-8')           #源文件
f_new = open('test2.txt','w',encoding='utf-8')       #复制后的文件
shutil.copyfileobj(f_old,f_new)                      #复制操作
shutil.copyfile("test2.txt","test3.txt")            #不需要读取文件,直接复制
shutil.copymode("test1.txt","test4.txt")            #仅拷贝权限。内容、组、用户均不变
shutil.copystat("test4.txt","test5.txt")            #拷贝状态的信息,包括:mode bits, atime, mtime, flags
shutil.copy("test5.txt","test6.txt")                #拷贝文件和权限
shutil.copytree("a","a_test")                       #递归拷贝目录以及其内容
shutil.rmtree("a_test")                             #删除递归目录
 
shutil.make_archive('test','zip','D:\pytest')     #对D:\pytest下的文件进行压缩
import zipfile
#使用zip压缩
m = zipfile.ZipFile('D:\pytest\day5.zip','w')      #以写的方式打开文件
m.write("test.zip")                        #传入文件
print("-----")
m.write("test1.txt")                      #再来一发
m.close()                                   #文件关闭
#解压
m = zipfile.ZipFile('D:\pytest\day4.zip','r')       #读的方式打开文件
m.extractall()                               #解压文件
m.close()                                    #解压完成关闭文件
 
import tarfile
#使用tar压缩
tar = tarfile.open("test.tar",'w')
tar.add('D:\pytest\day3.zip',arcname="day3.zip")
tar.add('D:\pytest\day4.zip',arcname="day4.zip")
tar.close()
#使用tar解压
tar = tarfile.open('test.tar','r')
tar.extractall()  # 可设置解压地址
tar.close()

  5.stelve模块: 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#!/usr/bin/env python
# -*- coding:utf-8 -*-
 
#由于不能多次dump或者load多次,效率低下
import shelve,datetime
#简单的将key,value数据持久化的一个模块,可以持久化任何pickle支持的python数据格式
d = shelve.open("test.txt")
info = {'name':'wanghui','age':25}
job = ['IT','WORKER','DESIGNER']
d["job"] = job
d["info"] = info
d['date'] = datetime.datetime.now()
d.close()
 
print(d.get('job'))    #获取job列表
print(d.get('info'))   #获取字典
print(d.get('date')   #获取时间

  6.xml模块

 需要处理的test.xml文件:

<?xml version="1.0"?>
<data>
    <country name="Liechtenstein">
        <rank updated="yes">2</rank>
        <year>2008</year>
        <gdppc>141100</gdppc>
        <neighbor name="Austria" direction="E"/>
        <neighbor name="Switzerland" direction="W"/>
    </country>
    <country name="Singapore">
        <rank updated="yes">5</rank>
        <year>2011</year>
        <gdppc>59900</gdppc>
        <neighbor name="Malaysia" direction="N"/>
    </country>
    <country name="Panama">
        <rank updated="yes">69</rank>
        <year>2011</year>
        <gdppc>13600</gdppc>
        <neighbor name="Costa Rica" direction="W"/>
        <neighbor name="Colombia" direction="E"/>
    </country>
</data>
View Code

 

 调用xml模块的方法处理xml文件:

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
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import xml.etree.ElementTree as ET
tree = ET.parse('test.xml')        #定义要处理的文件
first = tree.getroot()                #获取根节点参数
print(first.tag)                      #.tag就是将内存数据转化成可以读取的形式
 
#遍历xml文档
for second in first:                  #读取第二级节点
    print(second.tag,second.attrib)
    for third in second:              #读取第三级节点
        print(third.tag,third.attrib)
        for forth in third:
            print(forth.tag,forth.attrib)
#只遍历其中的year节点
for node in first.iter("year"):
    print(node.tag,node.text)
 
 
#修改xml文件
for node in first.iter('year'):
    new = int(node.text) + 1          #year + 1
    node.text = str(new)              #转换成str
    node.set("updated",'yes')       #添加新的属性
tree.write("test.xml")
 
#删除节点
for country in first.findall('country'):
    rank = int(country.find('rank').text)
    if rank > 50:
        first.remove(country)
tree.write('out.xml')
 
#创建xml文件
import xml.etree.ElementTree as ET
new_xml = ET.Element("namelist")            #声明namelist
personinfo = ET.SubElement(new_xml,'personinfo',attrib={'enrolled':'yes'})
name = ET.SubElement(personinfo,"age",atrrib={'checked','no'})
name.text = 'wanghui'
age = ET.SubElement(personinfo,"age",atrrib={'checked','no'})
sex = ET.SubElement(name,'sex')
age.text = '25'
personinfo2 = ET.SubElement(new_xml,'personinfo',attrib={'enrolled':'no'})
name = ET.SubElement(personinfo2,"age",atrrib={'checked','no'})
name.text = "alex"
age = ET.SubElement(personinfo2,"age")
age.text = '12'
 
ex = ET.ElementTree(new_xml)                   #生成文档对象
ex.write('test1.xml',encoding='utf-8',xml_declaration=True)
ET.dump(new_xml)       #打印生成的格式

  7.configparser模块:用于处理配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#!/usr/bin/env python
# -*- coding:utf-8 -*-
#configparser:处理配置文件
import configparser
conf = configparser.ConfigParser()      #生成configparser对象
conf["DEFAULT"] = {'ServerAliveInterval':'45',   #生成第一个实例
                   'Compression':'yes',
                   "CompressionLevel":9
}
conf['bitbucher.org'] = {}                         #第二个实例
conf['bitbucher.org'] ['User'] = 'hg'
 
conf["topsecret.server.com"] = {}               #第三个实例
topsecret = conf["topsecret.server.com"]
topsecret['Host Port'] = "52333"
topsecret['ForwardXll'] = 'no'
 
with open('example.ini','w',encoding='utf-8') as configfile:
    conf.write(configfile)                                    #写入配置文件

  其他操作:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# import configparser
# conf = configparser.ConfigParser()        #读取文件前戏
# conf.read('example.ini')
# print(conf.sections())
# print(conf.defaults())   #打印所有
# print(conf['bitbucher.org']['User'])   #读取bitbucher.org的user信息
# for key in conf['bitbucher.org']:      #利用循环读取
#     print(key)
 
#增删改查语法
import configparser
conf = configparser.ConfigParser()
conf.read('example.ini')
#读取一把
print(conf.sections())
print(conf['bitbucher.org']['User'])               #读取配置文件
#增删操作
ret = conf.remove_section('bitbucher.org')          #删除
conf.write(open('exaple.ini','w'))                  #写入文件

  8.hashlib模块:提供加密算法

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
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import hashlib
m = hashlib.md5()        #创建md5加密对象
m.update(b'hello')      #对象中插入数值
print(m.hexdigest())      #以十六进制打印md5值
m.update(b"it's me!!")
print(m.hexdigest())
m.update(b'as long as you love me!')
print(m.hexdigest())
#拼接
m2 = hashlib.md5()
m2.update(b"helloit's me!!"#更新之后的和拼接的md5一样
print(m2.hexdigest())
 
s1 = hashlib.sha512()
s1.update(b"helloit's me!!"#复杂算法
print(s1.hexdigest())
#带中文的加密
f1 = hashlib.md5()
f1.update("你妹呀!!".encode(encoding='utf-8'))
print(f1.hexdigest())
#更吊的版本
import hmac
k1 = hmac.new(b'wanghui',b'message')      #仅支持ascii码,不支持中文
print(k1.hexdigest())
print(k1.digest())
#带中文的加密
k2 = hmac.new("你大爷的!!".encode(encoding='utf-8'),"呵呵哒!".encode(encoding='utf-8'))
print(k2.hexdigest())

  

9.re模块:匹配字符串(模糊匹配)

Python 基础之大话 re

在使用re模块中主要会用到一下几个方法:

复制代码
re.match()
#从头匹配一个字符串
re.search()
#浏览全部字符串,匹配第一个符合规则的字符串
re.findall()
#将匹配到的所有内容都放在一个列表中
#re.finditer()
re.split()
re.sub()
复制代码

好,先简单的介绍一下re 模块中的基础知识:

    1.元字符 . ^ $ * + ? {} []  | () \  ,

    2.\的功能:

        #1.反斜杠后面跟元字符去除特殊功能
        #2.反斜杠后面跟普通字符实现特殊功能
        #3.引用序号对应的字组所匹配的字符串

有了上面的基本概念就可以写几个例子来说明一下问题了:

复制代码
a='abcd'.find('b')
b='abcd'.split('b')
c='abcd'.replace('ab','er')
d=re.findall('alex.w','aaaalexcw')#.为通配符,一个点只能表示一个字符.表示除去换行符以外任意一个字符
e=re.findall('^alex','alexaaalexcw')#^尖角符是用来控制开头的,而$是用来控制结尾的
f =re.findall('alex*','aaaalecw')#*为控制*符号前面的字符重复0~多次
g =re.findall('alex+','aaaalexxxxcw')#+为控制+符号前面的字符重复1~多次
h=re.findall('alex?','aaaalexcw')#?为控制?符号前面的字符重复0~1次
i = re.findall('alex{3,5}','aaaalexxxxxxxcw')#{}为控制{}符号前面的字符重复括号输入的次
g = re.findall('a[bc]d','aaaacdsssssacdvvdabcdf')
#[]表示的是字符集的意思字符集中的元素的关系是“或”的意思
#在字符集中[1-9]表示取1到9这几个数字都是可以的,[a-z]表示取a到z这些字母都是OK的
#[^xx]尖角^在字符集中的含义是取字符集中元素的“非”
复制代码

接下来对上面的常用方法进行举例:

复制代码
match方法中的有分组与无分组
#1.无分组
prigin = 'hello alex bcd alxe leg alex acd age19'
r = re.match('h\w+',prigin)
print(r.group()) #获取匹配到的所有结果
print(r.groups()) #获取模型中匹配到的分组结果
print(r.groupdict()) #获取模型中匹配到的分组结果

#2.有分组
#目的是提取匹配成功的指定内容(先匹配成功全部正则,再匹配成功的局部内容提取出来)
prigin = 'hello alex bcd alxe leg alex acd age19'
r = re.match('(h)\w+',prigin)
print(r.group()) #获取匹配到的所有结果,加括号后有无分组没有任何影响
print(r.groups()) #获取模型中匹配到的分组结果,加括号后会把加括号的内容提取出来
print(r.groupdict()) #获取模型中匹配到的分组结果
#在grouopdict()中需要一个键与值与之对应才可使用,在写法上如下例子所示
prigin = 'hello alex bcd alxe leg alex acd age19'
r = re.match('(?P<n1>h)(?P<n2>\w+)',prigin)
print(r.group()) #获取匹配到的所有结果,加括号后有无分组没有任何影响
print(r.groups()) #获取模型中匹配到的分组结果,加括号后会把加括号的内容提取出来
print(r.groupdict()) #获取模型中匹配到的分组结果


#search方法中的有分组与无分组
#1.无分组

prigin = 'hello alex bcd alxe leg alex acd age19'
r = re.search('a\w+',prigin)
print(r.group()) #获取匹配到的所有结果
print(r.groups()) #获取模型中匹配到的分组结果
print(r.groupdict()) #获取模型中匹配到的分组结果

#2.有分组
prigin = 'hello alex bcd alxe leg alex acd age19'
r = re.search('a(\w+).*(?P<name>\d)$',prigin)
print(r.group()) #获取匹配到的所有结果
print(r.groups()) #获取模型中匹配到的分组结果
print(r.groupdict()) #获取模型中匹配到的分组结果

'''

'''
#findall基本方法,做的都是groups()的东西
#1.寻找时是按照一个字符,一个字符的寻找,当找到符合要求的之后(相当于把符合要求的拿走),再从下一个字符开始寻找
r1 = re.findall('\d+\w\d+','a2b3c4d5')
print(r1)

#2.空的内容也可以匹配
r2 = re.findall('','a2b3c4d5')
print(r2)



#findall方法中的有分组与无分组

prigin = 'hello alex bcd alxe leg alex acd age 19'
r1 = re.findall('a\w+',prigin)
print(r1)
r2 = re.findall('(a\w+)',prigin)
print(r2)
r3 = re.findall('a(\w+)',prigin)
print(r3)
r4 = re.findall('(a)(\w+)',prigin)
print(r4)
r5 = re.findall('(a)(\w+(e))(x)',prigin)#从左到右,从外到内
print(r5)
#finditer 迭代过程可以加入的内容
prigin = 'hello alex bcd alxe leg alex acd age 19'
r6 = re.finditer('(a)(\w+(e))(?P<n1>x)',prigin)#从左到右,从外到内
print(r6)
for i in r6:
    print(i,i.group(),i.groups(),i.groupdict())



a = 'alex'
n = re.findall('(\w)(\w)(\w)(\w)',a)
print(n)
n = re.findall('(\w){4}',a)
print(n)
n = re.findall('(\w)*',a)#*代表有0~很多个,则当为0个时就会出现控格来补充
print(n)
n = re.findall('',a)
print(n)

'''
'''
#re.split方法的讲解(分割的目的)
origin = 'hello alex bcd alxe leg alex acd age 19'
origin.split('a')
print(origin.split('a'))#在字符串中分隔符的用法
#无分组
n = re.split('a\w+',origin,1)#第三个参数的意思是从头开始只分割一次,且此方法不保留分割的字符
#有分组
n1 = re.split('(a\w+)',origin)#在分割的字符上加上括号表示的是不仅需要按此字符分割而且还需要保留此字符
n2 = re.split('a(\w+)',origin)
print(n1 , n2)
'''


'''
#re.sub 是用来做替换用的

origin = '1jhjhjhj2hjhjhjk4hjhjhjkh5jkhjhjk6hkjk'
n1 = re.sub('\d+','DDD',origin,1)
n2 = re.sub('\d+','DDD',origin)
n3,count = re.subn('\d+','DDD',origin)
print(n1)
print(n2)
print(n3,count)
复制代码

 

 

posted on 2018-12-04 20:47  pf42280  阅读(183)  评论(0编辑  收藏  举报