subprocess, re, logging module

subprocess:

sub: child

process: Process

Call Popen will send commands to the user's terminal end desktop operating system

Get an object, the object contains the correct or incorrect results

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 module

import re

  • Character Group:

    [0-9] may be matched to a 0-9

    Note: The order must be prepared in accordance with the order of the ASCII value.

  • The main character is a dollar and a combination of

    \ W \ W: match alphanumeric and non-alphanumeric underscore underscore matches all.

    • \ D \ D: whether digital or non-digital can match.

    • \t: table

    • \ N: line feed

    • \ B: the end of the matching words, tank jasonk

    • ^: StartsWith
      - '^' out of use: to indicate the beginning.
      - [^]: indicates negated meaning.

    • $: endswith

    • ^ $: Precise match with the use of known, how to limit the length of a string or content.

    • |: Or. ab | abc if the first condition is satisfied, the abc will not be executed, how to solve this situation for the long EDITORIAL like, be sure to put a long front.

re module three of the more important ways:

    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) What are regular expressions re module?

Regular Expressions:
A regular expression is an independent technology, any language can use regular expressions,
regular expressions are a bunch of special character combinations come.

  • Character Group
  • Metacharacters
    • Combination

re module:
in python, if you want to use a regular expression, it must be achieved through re module.

2) Why use regular?
For example, to get "a bunch of string" in "some character",
a regular expression can help us to filter and extract character data you want.

Such as filter and get "tank"

'wafawrjkwagfiu21knriut8ankjfdgau0q92ru20yrisana tank wyqfwqrqyr9q 9'

Scenario:

  • Reptile: re, BeautifulSoup4, Xpath, selector
  • Data analysis filtering data: re, pandas, numpy ...
  • User name and password, phone Certification: checking the validity of input content
    • Username: na tank

3) How to use?
import re

logging module

Logging module is used, in general recorded in the software of the user.
def get_logger (user_type):

1. Load Configuration dictionaries log to the logging module

logging.config.dictConfig(LOGGING_DIC)

2. Get the log object

logger = logging.getLogger(user_type)
return logger

= get_logger Logger ( 'User')
logger.info ( 'message log')

When a test is performed to prevent the automatic import function module

IF name == ' main ':
perform the test module

Theory package

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产生的名称空间中。

Guess you like

Origin www.cnblogs.com/godlover/p/11892385.html