subprocess模块&re模块

subprocess模块

import subprocess #导入subprocess模块,该模块的作用为可以通过python代码向终端(cmd)发送命令

while True:      #进行循环,可以让用户重复的进行输入
   cmd_str = input('请输入终端命令:').strip()  #定义变量cmd_str
   obj = subprocess.Popen(
       cmd_str, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE
  )  #得到subprocess对象,语法格式为Popen(cmd命令, shell=True(默认为False改为True后,可以将python代码发送到cmd中
   # ),stdout=subprocess.PIPE, stderr=subprocess.PIPE)
   success = obj.stdout.read().decode('gbk') #定义变量success为上面对象中得到的正确的结果,然后进行判断分别输出对应的内容
   if success:
       print(success)
   error = obj.stderr.read().decode('gbk')  #定义变量error为上面对象中得到的错误的结果,然后进行判断分别输出对应的内容
   if error:
       print(error)

re模块

1.什么是正则表达式于re模块?

正则表达式是一门独立的技术,任何语言都可以使用正则表达式,它是由一堆特殊的字符组成的

主要类型有:字符串 元字符 以及元字符的组合使用

re模块:在python中,若要使用正则表达式,必须通过re模块来实现

2.为什么要使用正则?

正则表达式可以帮我们过滤,并提取出想要的字符数据,比如:获取‘一堆字符串’中的‘某些字符’

3.应用场景及使用?

爬虫

数据分析过滤数据

用户名与密码 手机认证:检测输入内容的合法性

#用正常的方法来做的话
while True:
   phone_number = input('请输入手机号:').strip()
   #用正常的方法来做的话,这里的条件要加很多
   if len(phone_number) == 11 and (phone_number.startswith('13')) or \
       phone_number.startswith('14') or phone_number.startswith('15') \
       or phone_number.startswith('19'):
       print('手机号码合法!')
       break
   else:
       print('手机号码不合法!')
       
#用正则表达式的话
import re
while True:
   phone_number = input('请输入手机号:').strip()
   #re.match(正则表达式, 需要过滤的字符串)
   if re.match('^(13|14|15|19)[0-9]{9}$', phone_number):
       print('合法')
       break
   else:
       print('不合法')

       
# 总结
# ^:代表'开头' $:代表'结束' |:代表'或' (13|14):可以获取一个值,判断是否是13或14
# {1}:需要获取一个值 限制数量 []:分组限制取值范围
字符组:
 - [0-9] 可以匹配到一个0-9的字符
 - [9-0]: 报错, 必须从小到大
 - [a-z]: 从小写的a-z
 - [A-Z]: 从大写A-Z
 - [z-A]: 错误, 只能从小到大,根据ascii表来匹配大小。
 - [A-z]: 总大写的A到小写的z。

 注意: 顺序必须要按照ASCII码数值的顺序编写。

#通过re获取指定的字符
import re
res = re.match('[a-z0-9]{2}', 'hcy8450') #这里[]代表取值的范围 {}代表的是取值的个数 后面的字符串代表目标对象(即要从哪里取出符合条件的值)
print(res)
print(res.group())
#结果为
<re.Match object; span=(0, 2), match='hc'>
hc

import re
res = re.match('[a-z0-9]{2}', 'Hcy8450')
print(res)
print(res.group())
#结果为
None
AttributeError: 'NoneType' object has no attribute 'group'
注意:#这里我们将后面的字符串第一个字符改为大写后,最后的结果出现了报错;是因为match这个函数是从一个字符串的开始位置匹配正则表达式,然而这个字符串的起始位置并不符合正则表达式,所以匹配失败,返回一个空变量,空变量并没有group()方法,所以调用不成功。

#组合使用用法
     - \w\W: 匹配字母数字下划线与非字母数字下划线,合在一起为匹配所有。
     - \d\D: 无论是数字或者非数字都可以匹配。
     - \t: table
     - \n: 换行
     - \b: 匹配单词结尾,tank  jasonk
     - ^: startswith
           - '^'在外面使用: 表示开头。
           - [^]: 表示取反的意思。
           
     - $: endswith
       
     - ^$: 配合使用叫做精准匹配,如何限制一个字符串的长度或者内容。
     - |: 或。ab|abc如果第一个条件成立,则abc不会执行,怎么解决,针对这种情况把长的写在前面就好了,一定要将长的放在前面。
     - [^...]: 表示取反的意思。
     - [^ab]: 代表只去ab以外的字符。
     - [^a-z]: 取a-z以外的字符。

re模块三种比较重要的方法:

#findall():可以匹配 "所有字符" ,拿到返回的结果,返回的结果是一个列表
res = re.findall('[a]', 'asasfqeknsjbdizbxhuc')
res1 = re.findall('[a-z]{3}', 'asasfqeknsjbdizbxhuc')
res2 = re.findall('[y|g]{3}', 'asasfqeknsjbdizbxhuc')
print(res)
print(res1)
print(res2)
#结果为
['a', 'a']
['asa', 'sfq', 'ekn', 'sjb', 'diz', 'bxh']
[]

#search(): 在匹配一个字符成功后,拿到结果后结束,不往后匹配,得到的结果为对象
import re
res = re.search('[a]', 'asasfqeknsjbdizbxhuc')
res1 = re.search('[a-z]{3}', 'asasfqeknsjbdizbxhuc')
res2 = re.search('[y|g]{3}', 'asasfqeknsjbdizbxhuc')
print(res)
print(res.group())
print(res1)
print(res1.group())
print(res2)
#结果为
<re.Match object; span=(0, 1), match='a'>
a
<re.Match object; span=(0, 3), match='asa'>
asa
None

#match():从匹配字符的开头匹配,若开头不是想要的内容,则返回None
import re
str1 = 'sean tank json'
res = re.findall('[a-z]{4}', str1)
res1 = re.search('[a-z]{4}', str1)
res2 = re.match('[a-z]{4}', str1)
print(res)
print(res1)
print(res1.group())
print(res2)
print(res2.group())
#结果为
['sean', 'tank', 'json']
<re.Match object; span=(0, 4), match='sean'>
sean
<re.Match object; span=(0, 4), match='sean'>
sean

 

 

猜你喜欢

转载自www.cnblogs.com/cyfdtz/p/11892358.html