• Python re模块 subprocess模块


    re模块    
    内部实现不是Python 而是调用了c的库

    re是什么
      正则 表达 式子
      就是一些带有特殊含义的符号或者符号的组合
    作用: 对字符串进行过滤
      在一对字符串中找到所关心的内容
      你就需要告诉计算机过滤规则是什么样
      通过什么方式来告诉计算机 就通过正则表达式

    re模块常用方法
    findall -------- 从左往右查找所有满足条件的字符 返回一个列表
    search -------- 返回第一个匹配的字符串 结果封装为对象 span=(0,5)匹配的位置 match匹配的值
    match --------- 匹配行首 返回值与search相同
    ----------------- 对于search match 匹配的结果通过group来获取
    compile -------- 将正则表达式封装为一个正则对象 好处是可以重复使用这个表达式

    第一步:
    学习正则表达式 各种符号所表示的含义
    各种符号
    常用的
    w s d . ^ $
    范围匹配
    [] a|b
    重复匹配
    {} + * ?
    分组
    ()
    常用的方法:
    findall
    search
    match
    sub
    split

    2.subprocess
    run 返回执行结果对象
    call 返回执行状态码
    Popen 返回的也是对象 out in err

    进程间的数据访问


    import re
    #待处理字符串
    # src = 'hades'
    #在字符串中查找所有满足条件的
    # print(re.findall('ad',src))
    
    # w 字母数字下划线
    # W 非字母数字下划线 与w相反
    # print(re.findall('w',src))
    # print(re.findall('W',src))
    
    # s 所有不可见字符
    # S 所有可见字符
    # print(re.findall('s',src))
    # print(re.findall('S',src))
    
    # d  所有数字可见
    # D  所有非数字可见
    # print(re.findall('d',src))
    # print(re.findall('D',src))
    
    # 特殊字符直接匹配
    # print(re.findall('
    ',src))
    # print(re.findall('	',src))
    
    # . 除了
    以外任意字符
    # print(re.findall('.',src))
    
    # s w d . 都是匹配单个字符
    #匹配重复字符  * + ? {}
    
    # * 前面的表达式出现任意次
    # print(re.findall('d*','1 12 aa'))
    
    # + 重复1次或多次
    # print(re.findall('d+','1 1221abc41515a aa'))
    
    # ? 重复0次或1次
    # print(re.findall('d?','aa bb a1c 1C1 哈哈哈 123'))
    
    # {n,m} 最少n次 最多m次
    # print(re.findall('d{1,3}','1 12 123 1234 123456'))
    
    # {n} 必须是n次
    # print(re.findall('[a-z]{3}','a aa aaa aaaa aaaaa'))
    
    # {,m} 最大m次 0-m
    # print(re.findall('[a-z]{,3}','a aa aaa aaaa aaaaa'))
    
    
    #匹配范围
    # |   0|1|2 或
    # print(re.findall('0|1|2','12413sdfg'))
    
    # [] 字符集合  括号内的符号不是整体
    # print(re.findall('[012]','1982asasa'))
    
    # 在范围匹配时使用 ^ 托字符表示取反
    # print(re.findall('[^0-9]','1982asasa'))
    
    #找出范围内 数字0-9 字母a-z A-Z  注意(减号只有在两个字符中间才代表范围,在两边都是普通字符)
    # print(re.findall('[0-9a-zA-Z]','1982+asasa'))
    
    # ^ 匹配行首
    # print(re.findall('^h','helhhlohh'))
    
    # $ 匹配行尾 注意:写在表达式后面
    # print(re.findall('s$','helhhlohs'))
    
    # 单词边界
    # print(re.findall('o\b','hello word hi hades'))
    
    #双斜杠?
    # print(re.findall('a\\c','aakakjac'))
    
    #贪婪匹配  * +   注意: 不是固定写法 是一种现象
    #会一直匹配到不满足条件为止  用问号来阻止贪婪匹配(匹配最少满足条件的字符数)
    # print(re.findall('w+?  ','dfgregersg'))
    # print(re.findall('w*?  ','dfgregersg'))
    
    # () 用于给正则表达式分组(group)
    #什么时候需要阻止贪婪
    # src = '<img src="www.baidupic.shuaiqi.jpg"><img src="www.baidupic.shuaiqi.jpg"><img src="www.baidupic.shuaiqi.jpg">'
    #请用正则表达式取图片地址
    # print(re.findall('src="(.+?)"',src))
    # 了解  加上?:  可以取消括号中的优先级
    # print(re.findall('src="(?:.+?)"',src))
    
    
    subprocess模块
    sub 子
    process 进程
    什么是进程
    正在进行中的程序 每当打开一个程序就会开启一个进程
    每个进程包含运行程序所需的所有资源
    正常情况下,不可以跨进程访问数据
    但是有些情况下就是需要访问别的进程数据 提供一个叫做管道的对象 专门用于跨进程通讯

    作用: 用于执行系统命令

    常用方法:
    run 返回一个表示执行结果的对象
    call 返回的执行的状态码
    总结: subprocess的好处是可以获取指令的执行结果
    subprocess执行指令时,可以在子进程中这样
  • 相关阅读:
    BI的相关技术和产品细分(转自娄工)
    SOA系列二:采用SOA的常见失误
    ASP.NET第三方控件网站
    BI名词字典
    VSS2005 添加文件夹方法!
    c#范型编程系列一(非原创)
    数据挖掘项目的生命周期
    SQLServer 2005开发与商业智能培训大纲
    CVSNT配置配置与在ECLIPSE中使用
    JS通用UI框架
  • 原文地址:https://www.cnblogs.com/earon/p/9478364.html
Copyright © 2020-2023  润新知