zoukankan      html  css  js  c++  java
  • subprocess,re,logging模块

    subprocess:

    ​sub:子

    ​process:进程

    ​调用Popen就会将用户的终端命令发送给本地操作系统的终端

    ​得到一个对象,对象中包含着正确或者错误的结果

    import subprocess
    while True:
    
        cmd_str = input('请输入终端命令:').strip()
        # Popen(cmd命令,shell=True,stdout=subprocess.PIPE,stderr = subprocess.PIPE)
        obj = subprocess.Popen(
            cmd_str, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE
        )
    
        success = obj.stdout.read().decode('gbk')
        if success:
            print(success, '正确的结果')
            break
    
        error = obj.stderr.read().decode('gbk')
        if error:
            print(error, '错误的结果')
            break
    
    

    re模块

    import re

    • 字符组:

      [0-9] 可以匹配到一个0-9的字符

      [9-0]: 报错, 必须从小到大

      [z-A]: 错误, 只能从小到大,根据ascii表来匹配大小。

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

    • 主要是元字符和组合使用

      wW: 匹配字母数字下划线与非字母数字下划线,匹配所有。

      • dD: 无论是数字或者非数字都可以匹配。

      • : table

      • : 换行

      • : 匹配单词结尾,tank jasonk

      • ^: startswith
        - '^'在外面使用: 表示开头。
        - [^]: 表示取反的意思。

      • $: endswith

      • ^$: 配合使用叫做精准匹配,如何限制一个字符串的长度或者内容。

      • |: 或。ab|abc如果第一个条件成立,则abc不会执行,怎么解决,针对这种情况把长的写在前面就好了,一定要将长的放在前面。

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

        findall(): ----> []
        可以匹配 "所有字符" ,拿到返回的结果,返回的结果是一个列表。
        'awfwaghowiahioawhio'  # a
        ['a', 'a', 'a', 'a']
    - search():----> obj ----> obj.group()
        'awfwaghowiahioawhio'  # a
        在匹配一个字符成功后,拿到结果后结束,不往后匹配。
        'a'
    
    - match():----> obj ----> obj.group()
        'awfwaghowiahioawhio'  # a
        'a'
        'wfwaghowiahioawhio'  # a
         None
        从匹配字符的开头匹配,若开头不是想要的内容,则返回None。
    
    import re
    str1 = 'sean tank json'
    # findall
    res = re.findall('[a-z]{4}', str1)
    print(res)  
    
    # search
    res = re.search('[a-z]{4}', str1)
    print(res)
    print(res.group())
    
    # match
    res = re.match('sean', str1)
    print(res)
    print(res.group())
    if res:
        print(res.group())
        
        
        
    ['sean', 'tank', 'json']
    <_sre.SRE_Match object; span=(0, 4), match='sean'>
    sean
    <_sre.SRE_Match object; span=(0, 4), match='sean'>
    sean
    sean
    
    

    1)什么是正则表达式与re模块?

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

    • 字符组
    • 元字符
      • 组合使用

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

    2)为什么要使用正则?
    比如要获取“一堆字符串”中的“某些字符”,
    正则表达式可以帮我们过滤,并提取出想要的字符数据。

    比如过滤并获取 “tank”

    'wafawrjkwagfiu21knriut8ankjfdgau0q92ru20yrisana tank wyqfwqrqyr9q 9'

    应用场景:

    • 爬虫: re, BeautifulSoup4, Xpath, selector
    • 数据分析过滤数据: re, pandas, numpy...
    • 用户名与密码、手机认证:检测输入内容的合法性
      • 用户名: na tank

    3)如何使用?
    import re

    logging模块

    是用来记录日志的模块,一般记录用户在软件中的操作。
    def get_logger(user_type):

    1.加载log配置字典到logging模块的配置中

    logging.config.dictConfig(LOGGING_DIC)

    2.获取日志对象

    logger = logging.getLogger(user_type)
    return logger

    logger = get_logger('user')
    logger.info('日志消息')

    防止导入模块时自动执行测试功能

    ​ if name == 'main':
    ​ 执行测试模块

    包的理论

    1.什么是包?
              包是一个带有__init__.py的文件夹,包也可以被导入,
              并且可以一并导入包下的所有模块。
    
    2.为什么要使用包?
           	 包可以帮我们管理模块,在包中有一个__init__.py, 由它来帮我们管理模块。
    3.怎么使用包?
        - import 包.模块名
            包.模块.名字
    
        - from 包 import 模块名
    
        - from 包.模块名 import 模块中的名字
    
    - 导入包时发生的事情:
        1.当包被导入时,会以包中的__init__.py来产生一个名称空间。
        2.然后执行__init__.py文件, 会将__init__.py中的所有名字添加到名称空间中。
        3.接着会将包下所有的模块的名字加载到__init__.py产生的名称空间中。
        4.导入的模块指向的名称空间其实就是__init__.py产生的名称空间中。
  • 相关阅读:
    Ensemble ID及转换
    FastQC及MultiQC整合使用
    Aspera下载安装使用
    RStudio代码折叠
    两样本检验
    单样本t检验,Python代码,R代码
    rMATS输出结果文件只有表头
    使用DiffBind 进行ATAC-seq peaks差异分析
    error while loading shared libraries: libcrypto.so.1.0.0: cannot open shared
    Python遗传算法初尝,火狐像素进化
  • 原文地址:https://www.cnblogs.com/godlover/p/11892385.html
Copyright © 2011-2022 走看看