zoukankan      html  css  js  c++  java
  • day21:正则函数&模块和包(import)

    正则函数

    1.search  通过正则匹配出第一个对象返回,通过group取出对象中的值

    # search   通过正则匹配出第一个对象返回,通过group取出对象中的值
    strvar = "1+2 3*4"
    obj = re.search("d+(.*?)d+",strvar)
    print(obj)
    # 返回匹配到的内容(匹配到一个就返回)
    res = obj.group()
    print(res) # <_sre.SRE_Match object; span=(0, 3), match='1+2'>
    # 返回分组里面的内容,类型是元组
    tup = obj.groups()
    print(tup[0]) # 1+2

    2.match  验证用户输入内容

    # match    验证用户输入内容(了解)
    """当search函数里面的正则表达式前面加上^ 等价于 matth的用法"""
    strvar = "a13566668888"
    obj = re.search("^d+",strvar)
    print(obj)
    # print(obj.group())
    obj = re.match("d+",strvar)
    print(obj)
    # print(obj.group())

    3.split  切割

    # split    切割
    strvar = "alex|xboyww&wusir%ritian"
    res = re.split("[|&%]",strvar)
    print(res) # ['alex', 'xboyww', 'wusir', 'ritian']
    
    strvar = "alex234234xboyww6786wusir78967896ritian"
    res = re.split("d+",strvar) # ['alex', 'xboyww', 'wusir', 'ritian']
    print(res)

    4.sub  替换

    # sub      替换 
    """sub(正则,要替换的字符,原字符串[,次数])"""
    strvar = "alex|xboyww&wusir%ritian"
    res = re.sub("[|&%]","-",strvar)
    print(res) # alex-xboyww-wusir-ritian
    strvar = "alex|xboyww&wusir%ritian"
    res = re.sub("[|&%]","-",strvar,2)
    print(res) # alex-xboyww-wusir-ritian

    5.subn  替换

    # subn     替换 (用法和sub一样,区别在于返回的是元组 (结果,次数)  )
    strvar = "alex|xboyww&wusir%ritian"
    res = re.subn("[|&%]","-",strvar)
    res = re.subn("[|&%]","-",strvar,1)
    print(res)

    6.finditer  返回字符串中响应内容 返回迭代器

    # finditer 匹配字符串中相应内容,返回迭代器[迭代器中包含的是对象]
    from collections import Iterator , Iterable
    strvar = "jkasdfjkadfjk1234asfj2342kfa"
    it = re.finditer("d+",strvar)
    print(isinstance(it,Iterator))
    
    # 获取迭代器里面的内容
    for i in it:
        print(i.group())

    7.compile  指定一个统一的匹配规则

    # compile  指定一个统一的匹配规则
    """
    正常情况下,正则表达式编译一次,执行一次.
    如果想要编译一次,多次使用的话,使用compile
    
    compile 可以编译一次,终身受益.节省系统的资源
    """
    strvar = "jksdjdfsj72343789asdfaj234"
    pattern = re.compile("d+")
    print(pattern)
    lst = pattern.findall(strvar)
    print(lst)
    obj = pattern.search(strvar)
    print(obj.group())

    8.正则表达式修饰符

    # ### 正则表达式修饰符
    # re.I 使匹配对大小写不敏感
    strvar = "<h1>72347923489</H1>"
    pattern = re.compile(r"<h1>(.*?)</h1>",flags=re.I)
    obj = pattern.search(strvar)
    print(obj)
    print(obj.group())
    
    
    # re.M 使每一行都能够单独匹配(多行),影响 ^ 和 $
    strvar = """<h1>72347923489</H1>
    <p>72347923489</p>
    <li>72347923489</li>
    """
    pattern = re.compile("^<.*?>(?:.*?)<.*?>$",flags=re.M)
    lst = pattern.findall(strvar)
    print(lst)
    
    
    # re.S 使 . 匹配包括换行在内的所有字符
    strar = """give
    1234234234mefive
    """
    pattern = re.compile("(.*?)mefive",flags=re.S)
    obj = pattern.search(strar)
    print(obj)
    print(obj.group())
    
    # 可以加多个修饰符 通过| 拼接
    """
    pattern = re.compile("(.*?)mefive",flags=re.S|re.I|re.M)
    """

    模块和包

    模块部分

    1.import导入

    # 导入一次,终身受益,不会重复导入
    """
    import mymodule
    import mymodule
    import mymodule
    
    """"""
    # 模块.变量
    print(mymodule.dog)
    # 模块.函数
    mymodule.xboyww()
    # 模块.类
    print(mymodule.MyClass().name)

    2.导入任意路径下的模块

    # ### 2.导入任意路径下的模块
    """自定义模块时,不能使用中文,也不能使用已存在的模块名,会被覆盖."""
    import sys
    print(sys.path)
    """
    # 把路径添加到系统环境变量path当中,
    # 执行命令时,系统会自动按照路径找到模块,从而引入
    # 如果找不到当前模块所在的路径,直接报错
    """
    
    
    # 在windows中执行
    sys.path.append(r"E:python31_gx")
    import mymodule2
    print(mymodule2.ceshi100)
    
    import mymodule2 as m2
    print(m2.ceshi200)
    
    
    """
    # [linux]
    ['/mnt/hgfs/python31_gx/day21/import_bao',
    '/home/wangwen/PycharmProjects/untitled', 
    '/usr/lib/python36.zip', 
    '/usr/lib/python3.6', 
    '/usr/lib/python3.6/lib-dynload', 
    '/usr/local/lib/python3.6/dist-packages', 
    '/usr/lib/python3/dist-packages'
    ]
    
    # [windows]
    ['E:\python31_gx\day21\import_bao', 
    'E:\py_mylianxi', 
    'E:\py_mylianxi\venv\Scripts\python36.zip', 
    'C:\Users\wangwen\AppData\Local\Programs\Python\Python36\DLLs', 
    'C:\Users\wangwen\AppData\Local\Programs\Python\Python36\lib', 
    'C:\Users\wangwen\AppData\Local\Programs\Python\Python36', 
    'E:\py_mylianxi\venv', 'E:\py_mylianxi\venv\lib\site-packages'
    ]
    """

    3.from ... import ...

    # ### 3.from ... import ... 从 ... 导入 ... 
    # 导入单个
    from mymodule import dog
    print(dog)
    
    # 导入多个
    from mymodule import cat,xboyww
    print(cat)
    xboyww()
    
    # 导入所有
    """ * 代表所有 """
    from mymodule import *
    print(cat)
    xgirl()
    
    # 导入的同时,起别名
    from mymodule import xboyww as ww
    ww()
    

    *** 关于设置*号的导入范围:

    # 可以设置*号的导入范围
    from mymodule import *
    print(cat)
    # xboyww() error
    '''可以在mymodule模块中设置__all__ = ["dog","cat"]来指定*号的导入范围'''

    4.__name__ 魔术属性的使用

    返回模块名字的魔术属性 __name__
    如果当前文件是直接运行的,返回__main__
    如果当前文件是间接导入的,返回当前文件名(模块名)

    文件直接被执行的时候返回__main__
    当成模块被导入的时候,返回模块名本身

    包的部分

    关于包,需要注意的点:

    1.文件就是模块,文件夹就是包
    2.__init__.py 对包(文件夹)进行初始化的脚本文件
    导入包的时候,系统自动调用__init__.py文件,把init文件里面成员进行导入
    3.可以通过__init__间接导入其他模块

    1.import导入包

    # ### (一)import 导入包的使用
    # 1.获取包初始化文件中的成员
    import package1
    print(package1.ceshi301)
    
    # 2.导入包下的某些模块
    # 方法一
    import package1.mypath
    package1.mypath.join()
    
    # 方法二(模拟os.path.join写法)
    import package1
    package1.mypath.join()
    package1.mypath.getsize()
    '''
    注意:在方法二的情况下,需要在包内的__init__.py中写入from package1 import mypath
    相当于通过__init__文件间接导入了join和getsize方法
    '''

    2.from ... import 从包导入相应成员

    # 从包当中导入成员属性(__init__)
    from package1 import ceshi301
    print(ceshi301)
    # 从包当中导入模块
    from package1 import mypath
    mypath.join()
    # 引入包下的模块下的具体成员
    from package1.mypath import getsize
    getsize()
    # 在引入的同时,起别名
    from package1.mypath import getsize as gs , join as j
    gs()
    j()
    
    # 导入所有,可以指定*号引入的范围
    from package1 import *
    print(ceshi301)
    # print(ceshi302) error

    3.单入口模式(相对导入)

    # ### (三) 单入口模式(相对导入)
    import package2.pkg1.pgone_1 as ppp1
    # print(ppp1.ceshi1000)
  • 相关阅读:
    cgo中调用有函数指针参数的C函数
    为什么要微服务架构服务化?
    学习刘杨人像后期笔记纪要
    错误日志表设计
    通过jstack日志分析和问题排查
    【JVM】jmap命令详解----查看JVM内存使用详情
    【JVM】jstat命令详解---JVM的统计监测工具
    介绍几种常用的监控工具
    【转】Pinpoint分布式系统性能监控工具
    【转】Skywalking概述
  • 原文地址:https://www.cnblogs.com/libolun/p/13411735.html
Copyright © 2011-2022 走看看