zoukankan      html  css  js  c++  java
  • Python常用模块(四)

    一.re模块

      正则表达式时计算机科学的一个概念,正则表达式通常被用来检索,替换那些符合某个模式的文本,大多数程序设计语言都支持利用正则表达式进行字符串操作.

      正则就是用一些具有特殊含义的符号组合到一起来描述字符或者字符串的方法,或者说正则就是用来描述一类事物的规则.它内嵌在python中,并通过re模块来实现,正则表达式模式被编译成一系列的字节码,然后由C编写的匹配引擎执行.re模块的作用是对字符串进行过滤,在一字符串中如果想要找到想要得到的内容就需要告知其过滤规则,这个过滤规则就是正则表达式.

    常用匹配模式:

      

    import re
    # 待处理字符串
    str1 = 'abc-d$eaf+ 
      1*/__g12a3'
    # findall()在字符串中查找所有满足条件的
    # w查找字母数字下划线  W查找非字母数字下划线
    print(re.findall('w',str1))
    print(re.findall('W',str1))
    
    # s查找所有不可见字符 S查找所有可见字符
    print(re.findall('s',str1))
    print(re.findall('S',str1))
    
    # d查找任意数字 D查找任意非数字
    print(re.findall('d',str1))
    print(re.findall('D',str1))
    
    # 
    
    print(re.findall('
    ',str1))
    
    # .匹配除了换行符的任意字符
    print(re.findall('.',str1))
    
    # swd 都是匹配单个字符
    # 匹配重复字符* + ? {}
    # *前面的表达式匹配0次或多次
    print(re.findall('wd*',str1))
    
    # +前面的表达式匹配1次或多次
    print(re.findall('d+','1 11 asf 2'))
    
    # ?前面的表达式匹配1次或0次
    print(re.findall('d?','1 111'))
    
    # {m,n}最少m次,最多n次
    print(re.findall('d{1,3}','1 12111'))
    #{m}必须是m次
    print(re.findall('[a-z]{3}','aaa aa a aa aaa aaaa'))
    
    # 从字符中找到左右的0或1或2
    # | 匹配范围
    print(re.findall('0|1|2','123af45ad60d21'))
    # []字符集合 中括号中的符号不是整体是单个字符
    print(re.findall('[012]','123af45ad60d21'))
    # 在范围匹配时使用脱字符表示取反
    print(re.findall('[^0-9]','123af45ad60d21'))
    # 请找出所有的数字0-9和字母a-z A-Z 注意 减号只有在两个字符中间才有范围的意思
    print(re.findall('[0-9a-zA-Z]','123+_lk#$a'))
    
    #  ^ 匹配行首
    print(re.findall('^h','hellohh'))
    # $ 匹配行尾 注意$写在表达式的后面
    print(re.findall('ha$','hellohha'))
    
    # 匹配单词末尾
    print(re.findall(r'hB','elloh wohrld okhi'))
    
    # 贪婪匹配 * +
    # 会一直匹配到不满足条件为止 用问好来阻止贪婪匹配(匹配最少满足条件的字符数)
    print(re.findall('w*?','sfasdefd'))
    
    src = "<img src='www.baidu.jpg'><img src='www.baidu1.jpg'><img src='www.baidu2.jpg'>"
    # ()用于给正则表达式分组(group) 不会改变原来的表达式逻辑意义
    # 优先取出括号内的内容 ?:取消括号的优先级
    print(re.findall("src='(.+?)'",src))

    二.subprocess模块

      subprocess模块是python2.4中新增的一个模块,它允许你生成新的进程,连接到它们的in/out/err管道,并获取他们的返回码.

      subprocess模块中常用函数:

        subprocess.run()      Python 3.5中新增的函数。执行指定的命令,等待命令执行完成后返回一个包含执行结果的CompletedProcess类的实例

        subprocess.call()      执行指定的命令,返回命令执行状态,其功能类似于os.system(cmd)。

        subprocess.Popen()  该类用于在一个新的程序中执行一个子程序.上面的函数都是基于subprocess.Popen类实现的

      实例代码:

    import subprocess
    res = subprocess.run('tasklist',shell=True,stdout=subprocess.PIPE)
    print(res.stdout.decode('gbk'))
    
    print(res.stderr)
    #
    res = subprocess.call('tasklist',shell=True)
    print(res)
    # 第一个进程a读取tasklist的内容 将数据交给另一个进程b 另一个进程b将数据写到文件中
    res1 = subprocess.Popen('tasklist',stdout=subprocess.PIPE,shell=True,stderr=subprocess.PIPE)
    res2 = subprocess.Popen('findstr cmd',stdout=subprocess.PIPE,shell=True,stderr=subprocess.PIPE,stdin=res1.stdout)
    print(res2.stdout.read().decode('gbk'))
  • 相关阅读:
    CNN comprehension
    Gradient Descent
    Various Optimization Algorithms For Training Neural Network
    gerrit workflow
    jenkins job配置脚本化
    Jenkins pipeline jobs隐式传参
    make words counter for image with the help of paddlehub model
    make words counter for image with the help of paddlehub model
    git push and gerrit code review
    image similarity
  • 原文地址:https://www.cnblogs.com/louyefeng/p/9474839.html
Copyright © 2011-2022 走看看