常用模块(四)

一、subprocess模块

1、subprocess为子流程模块,用于执行系统命令,该模块在Python全栈开发中不常用

2、常用方法

  run    返回一个表示执行结果的对象
  call    返回的执行的状态码

 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)

run方法与callt方法

3、Popen()方法

 #  第一个进程a读取tasklist的内容,将数据交给另一个进程b,进程b将数据写到文件中
res1 = subprocess.Popen("tasklist",stdout=subprocess.PIPE,shell=True,stderr=subprocess.PIPE) res2 = subprocess.Popen("echo >a.txt", stdout=subprocess.PIPE, shell=True, stderr=subprocess.PIPE,stdin=res1.stdout) print(res2.stdout.read().decode("gbk"))

二、re模块

1、什么是re

re是正则表达式,正则表达式是一些带有特殊意义的符号或符号的组合

2、常用匹配模式

最常用的有:
  单个字符匹配:
    \w 字母数字下划线 
  \s 所有不可见字符(\n \t \f)
   \d 所有数字
    . 除了\n以外的所有字符
    ^ 字符串的开头,写在表达式的前面
    $    字符串的末尾,写在表达式的后面   范围匹配:
    [abc] 括号内的一个字符
a|b a或b   重复匹配
    {} {,m}:0到m之间, {m,n}:m到n之前 , {m}:必须是m
    + 匹配1个或多个,会一直匹配到不满足条件为止,用“?”问号来阻止贪婪匹配(匹配最少满足条件的字符数)
* 匹配0个或多个,会一直匹配到不满足条件为止,用“?”问号来阻止贪婪匹配(匹配最少满足条件的字符数)
? 匹配1个或0个   分组
    ()  匹配括号内的表达式,提取括号中的表达式,不会改变原来的表达式逻辑意义
  取消分组
    (?: )

 import re

 src = 'abc_d12 3d d5s\nd'

 # \d 所有数字
print(re.findall('\d', src))
# ['1', '2', '3', '5'] # \w 所有数字字母下划线
print(re.findall('\w', src))
# ['a', 'b', 'c', '_', 'd', '1', '2', '3', 'd', 'd', '5', 's', 'd'] # \s 所有不可见字符
print(re.findall('\s', src))
# [' ', ' ', '\n'] # . 所有除了\n以外的字符
print(re.findall('.', src))
# ['a', 'b', 'c', '_', 'd', '1', '2', ' ', '3', 'd', ' ', 'd', '5', 's', 'd']? # ^ 匹配行首指定字符
print(re.findall('^a', src))
# ['a'] # $ 匹配行尾指定的字符
print(re.findall('d$', src))
# ['d'] # [abd] 匹配中括号内的任意一个字符(a到d,1到3)
print(re.findall('[a-d1-3]', src))
# ['a', 'b', 'c', 'd', '1', '2', '3', 'd', 'd', 'd']
print(re.findall('[^a-d]', src)) # 匹配除了a-d以外的字符
# ['_', '1', '2', ' ', '3', ' ', '5', 's', '\n'] # {m,n}
print(re.findall('d{1,3}', 'd dd ddd dddd'))
# ['d', 'dd', 'ddd', 'ddd', 'd'] # + 1个或多个
print(re.findall('d+', 'd dd ddd dddd'))
# ['d', 'dd', 'ddd', 'dddd'] # * 0个或多个
print(re.findall('d*', 'd dd ddd dddd'))
# ['d', '', 'dd', '', 'ddd', '', 'dddd', ''] # ? 0个或1个
print(re.findall('\d?', 'd 21dd_\n4'))
# ['', '', '1', '2', '', '', '', '', '4', '']

常用符号

 # 贪婪匹配  *  +    不是固定的特殊符号  只是一种现象
# 会一直匹配到不满足条件为止 用问号来阻止贪婪匹配(匹配最少满足条件的字符数) print(re.findall("\w+?", "ajshsjkdsd"))
# ['a', 'j', 's', 'h', 's', 'j', 'k', 'd', 's', 'd'] print(re.findall("\w*?", "ajshsjkdsd"))
# ['', '', '', '', '', '', '', '', '', '', ''] print(re.findall("\w+?s", "ajshsjkdsd"))
# ['ajs', 'hs', 'jkds'] print(re.findall("\w*?s", "ajshsjkdsd"))
# ['ajs', 'hs', 'jkds']

贪婪匹配和阻止贪婪

3、re模块的常用方法

(1).findall        从左往右查找所有满足条件的字符 返回一个列表
(2).search      返回第一个匹配的字符串,结果封装为对象
(3).match(不常用)   匹配行首, 返回值与search相同
(4).compile(不常用)   将正则表达式封装为一个正则对象,可以重复使用这个表达式

 import re

 print(re.findall('\w', src))
# ['a', 'b', 'c', '_', 'd', '1', '2', '3', 'd', 'd', '5', 's', 'd'] print(re.search('hello','weqwe hello dddd helllo dd'))
# <_sre.SRE_Match object; span=(6, 11), match='hello'> print(re.match("hello"," world hello python"))
# None

方法

4、分组

分组是从左边第一个左括号起,,index逐步增加,下面的1-4就是res=re.match(r"((a(b)c)(def))","abcdef")

 ts = "abcdef"
reg = r"((a(b)c)(def))"
regex = re.compile(reg)
res = regex.match(ts)
print(res)
print(res.span()) # 匹配的结果的区间
print(res.group(0)) # abcdef
print(res.group(1)) # 1 -> 第一个() abcdef
print(res.group(2)) # abc
print(res.group(3)) # b
print(res.group(4)) # def
print(res.groups()) # ('abcdef','abc','b','def')

常用内置模块(四)——subprocess、re的更多相关文章

  1. Python第五章__模块介绍,常用内置模块

    Python第五章__模块介绍,常用内置模块 欢迎加入Linux_Python学习群  群号:478616847 目录: 模块与导入介绍 包的介绍 time &datetime模块 rando ...

  2. 简学Python第五章__模块介绍,常用内置模块

    Python第五章__模块介绍,常用内置模块 欢迎加入Linux_Python学习群  群号:478616847 目录: 模块与导入介绍 包的介绍 time &datetime模块 rando ...

  3. Python17个常用内置模块总结

    Python17个常用内置模块总结 1.getpass 2.os 3.sys 4.subprocess 5.hashlib 6.json 7.pickle 8.shutil 9.time 10.dat ...

  4. 常用内置模块(一)——time、os、sys、random、shutil、pickle、json

    常用内置模块 一.time模块 在python中,时间分为3种       1.时间戳: timestamp,从1970年1月1日到现在的秒数, 主要用于计算两个时间的差    2.localtime ...

  5. Python基础之模块:2、包的使用和软件开发目录规范及常用内置模块

    目录 一.包的使用 1.什么是包 2.包的具体使用 1.常规导入 2.直接导入包名 二.编程思想转变 1.面条阶段 2.函数阶段 3.模块阶段 三.软件目录开发规范 1.bin 2.conf 3.co ...

  6. iOS中常用的四种数据持久化方法简介

    iOS中常用的四种数据持久化方法简介 iOS中的数据持久化方式,基本上有以下四种:属性列表.对象归档.SQLite3和Core Data 1.属性列表涉及到的主要类:NSUserDefaults,一般 ...

  7. Python常用内置模块之xml模块

    xml即可扩展标记语言,它可以用来标记数据.定义数据类型,是一种允许用户对自己的标记语言进行定义的源语言.从结构上,很像HTML超文本标记语言.但他们被设计的目的是不同的,超文本标记语言被设计用来显示 ...

  8. python常用模块之subprocess

    python常用模块之subprocess python2有个模块commands,执行命令的模块,在python3中已经废弃,使用subprocess模块来替代commands. 介绍一下:comm ...

  9. 【温故知新】——原生js中常用的四种循环方式

    一.引言 本文主要是利用一个例子,讲一下原生js中常用的四种循环方式的使用与区别: 实现效果: 在网页中弹出框输入0   网页输出“欢迎下次光临” 在网页中弹出框输入1   网页输出“查询中……” 在 ...

  10. Java中常用的四种线程池

    在Java中使用线程池,可以用ThreadPoolExecutor的构造函数直接创建出线程池实例,如何使用参见之前的文章Java线程池构造参数详解.不过,在Executors类中,为我们提供了常用线程 ...

随机推荐

  1. linux搭建集群

    磁盘分布 /boot 系统启动时需要的内存(200m) / 系统的可用磁盘大小(10240m) swap 交换内存 建议和内存一致(200m) 名字统一设置: 虚拟机名字 计算机名字 网络中的名字 默 ...

  2. MySQL高级学习笔记(二):mysql配置文件、mysql的用户与权限管理、mysql的一些杂项配置

    文章目录 mysql配置文件 二进制日志log-bin 错误日志log-error 数据文件 两系统 Myisam存放方式 innodb存放方式 如何配置 mysql的用户与权限管理 MySQL的用户 ...

  3. ubuntu+VS code+launch.json+task.json

    1.ubuntu->vs code . 通过官方PPA安装Ubuntu make sudo add-apt-repository ppa:ubuntu-desktop/ubuntu-make s ...

  4. 字母加密-C基础

    输入一个英文小写字符和正整数k(k<26),将英文字母加密并输出.加密思想:将每个字母c加一个序数k, 即用它后面的第k个字母代替,变换公式:c = c + k.如果字母为z,则后一个字母是a, ...

  5. [USACO10MAR]伟大的奶牛聚集Great Cow Gat…

    题目描述 Bessie is planning the annual Great Cow Gathering for cows all across the country and, of cours ...

  6. CTU OPEN 2017 Punching Power /// 最大独立集

    题目大意: 给定n 给定n个机器的位置 要求任意两个机器间的距离至少为1.3米 求最多能选择多少个机器 至少为1.3米 说明若是位于上下左右一步的得放就不行 将机器编号 将不能同时存在的机器连边 此时 ...

  7. 实习记——《Rethink》

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/gmszone/article/details/30045055 最终能够在和自己的电脑上写下这些字了 ...

  8. CVE-2019-14287_sudo权限提升

    影响范围 sudo是linux下普通用户使用root权限的命令,sudo配置文件中使用 ALL 语句,可以使普通账号通过vim执行root权限命令. 影响 sudo 1.8.28 之前的所有版本. 漏 ...

  9. bash命令根据历史记录补全

    用zsh比较方便的一个功能是在找之前用过的命令时可以先输入一部分命令作为过滤条件, 比如,想找 docker run 开头的历史命令,只需要键入 docker run 然后按 ↑ 进行选择. 但是在用 ...

  10. log库

    https://github.com/orocos-toolchain/log4cpp https://github.com/search?q=glog zlog https://github.com ...