day19-re module, sys module, print progress bar, subprocess (including class review)

Review of last class

1. Serialization/Deserialization

    Data in memory---serialization--->intermediate format (stored to hard disk/transmitted over network)

    Advantages of json
        : it can be cross-platform

        Disadvantage: Not all Python types are recognized (sets, tuples are not recognized)


    pickle
        advantage: can identify all python types

        Disadvantage: not cross-platform

    import json
    dic={'x':1}
    json_str=json.dumps(dic)

    json_loads(json_str)

    with open('db.json','wt',encoding='utf-8') as f:

        json.dump(dic,f)

    with open('db.json','rt',encoding='utf-8') as f:

        json.load(f)

2. Time module
    timestamp time.time()
    formatted string time.strftime('%Y %m %d %H:%M:%S %p')

    Structured time time.localtime() time.gmtime()
    datetime.datetime.now()
    datetime.datetime.fromtimestamp(123123123)

    datetime.datetime.now () + datetime.timedelta (hours = -3)

01 re module

1. What is a regular

    Regular is a set of rules composed of a series of characters with special meanings. The rules are used to describe strings with a certain characteristic. Regular is used to match substrings that meet the rules in a large string.

2. Why use regular
    1. User registration

    2. Crawler program


# print(re.findall('\w','hello 123_ */-='))
# print(re.findall('\W','hello 123_ */-='))
# print(re.findall('\s','hell\no 12\t3_ */-='))
# print(re.findall('\S','hell\no 12\t3_ */-='))
# print(re.findall('\d','hell\no 12\t3_ */-='))
# print(re.findall('\D','hell\no 12\t3_ */-='))
# print(re.findall('\n','hell\no 12\t3_ */-='))
# print(re.findall('\t','hell\no 12\t3_ */-='))
# print(re.findall('l','hell\no 12\t3_ */-='))


# print(re.findall('egon','my name is egon,egon is beautiful'))
#                                                      egon
# print(re.findall('^egon','egon my name is egon,egon is beautiful'))
# print(re.findall('egon$','egon my name is egon,egon is beautifulegon1'))
# be

# Repeat match
# .: Match any character other than a newline
# print(re.findall('ac','abc a1c aac asd aaaaac a*c a+c abasd')) #['abc','a1c' ,'aac','aac','a*c','a+c']
# ac
# print(re.findall('ac','abc a1c aac a\nc asd aaaaac a*c a+c abasd ',re.DOTALL))


# []: Match a character that belongs to the character specified in the brackets
# print(re.findall('a..c','abc a1 c aac asd aaaaac a *c a+ c abasd ='))
# print(re.findall('ac','abc a1 c aac aAc aBc asd aaaaac ac a/ca *c a+c abasd = a1c a2c'))
# print(re.findall(' a[az]c','abc a1 c aac aAc aBc asd aaaaac ac a/ca *c a+c abasd = a1c a2c'))
# print(re.findall('a[AZ]c','abc a1 c aac aAc aBc asd aaaaac a-c a/c a *c a+c abasd = a1c a2c'))
a ab abbb abbbb a1bbbb a-123 ')) # ab? # ['a', 'ab', 'ab', 'ab', 'a', 'a']














# print(re.findall('ab{0,1}','a ab abbb abbbb a1bbbb a-123'))


# +: must be used in conjunction with other characters, which means the character on the left appears 1 or infinite times
# print (re.findall('ab+','a ab abbb abbbb a1bbbb a-123'))
# ab+
# ['ab','abbb','abbbb']
# print(re.findall('ab{1,} ','a ab abbb abbbb a1bbbb a-123'))


# {n,m}: must be used with other characters
# print(re.findall('ab{1,3}','a ab abbb abbbb a1bbbb a- 123'))
# ab{1,3}
# ['ab','abbb','abbb']


# .*: greedy match
# print(re.findall('a.*c','ab123adfc1134124123adasfc123123'))

# .*?: non-greedy match
# print(re.findall('a.*?c','ab123adfc1134124123adasfc123123'))
# a.*?c

#(): grouping
# print(re.findall('expression="(.*?)"','expression="1+2+3/4*5" egon="beautiful"'))
#                                       expression=".*?"

# print(re.findall('href="(.*?)"','<p>段落</p><a href="https://www.sb.com">点我啊</a><h1>标题</h1><a href="https://www.sb.com">点我啊</a>'))

#|:
# print(re.findall('a|b','ab123abasdfaf'))
#                        a|b

# print(re.findall('compan(?:ies|y)','Too many companies have gone bankrupt, and the next one is my company'))

#companies   company

# print(re.findall(r'a\\c','a\c a1c aAc aac'))
# print(re.findall('a\\\\c','a\c a1c aAc aac'))


# print(re.findall('ale(x)','alex is SB,alex is bigSB'))
# print(re.search('alex','alex is SB,alex is bigSB'))
# print(re.search('ale(x)','alex is SB,alex is bigSB').group()) #alex, only until the first match is found and then returns an object containing matching information, the object The matched string can be obtained by calling the group() method, and if the string does not match, it will return None.

# print(re.search('abcdefg','alex is SB,alex is bigSB'))

# print(re.search('^alex','123alex is SB,alex is bigSB'))
# print(re. match('alex','123alex is SB,alex is bigSB'))

# l='egon:18:male'.split(':')
# print(l)
# l1=re.split('[ :/ -]','ab/c egon:18:male xxx') #-Because of its special function, it can only take effect if it is placed at the first or at the end

# print(l1)

# print(re.sub('[az]+ xx','yxp','lxx is good,sb is lllxx wxx is good cxx is good')) #sub is replacement, replace [az]+xx with yxp

#                                                   [a-z]+xx
结果:yxp is good,sb is yxp yxp is good yxp is good


pattern=re.compile('alex')
print(pattern.findall('alex is SB,alex is bigSB'))

print(pattern.search('alex is SB,alex is bigSB'))

02 sys module

import sys
sys.path
sys.argv #Used to receive the parameters followed by the Python interpreter to execute the Py file
#For example: python cp.py argv1 argv2 arg3
#sys.argv=['cp.py','argv1','argv2' ,'argv3']

03 Print progress bar

print('[%-50s]' %'#')
# print('[%-50s]' %'##')
# print('[%-50s]' %'###')
# print( '[%-50s]' %'####')
# print('[%-50s]' %'#####') split from %s and add function -->%-50s, 50 is the grid number

# 1. Control the width of the printing progress bar
# res='[%%-%ds]' %50    #The second % sign means canceling the special meaning of the first %
# print(res %'#')
# print( res %'##')
# print(res %'###')
# print(res %'####')

# print(res %'#####')

#2. No line break + jump back to the beginning of the line to print
# import time
# print(('\r[%%-%ds]' %50) %'#',end='')   
# time.sleep(0.5)
# print(('\r[%%-%ds]' %50) %'##',end='')
# time.sleep(0.5)
# print(('\r[%%-%ds]' %50) %'###',end='')
# time.sleep(0.5)
# print(('\r[%%-%ds]' %50) %'####',end= '')
# time.sleep(0.5)

# print(('\r[%%-%ds]' %50) %'#####',end='')


# Job: print progress bar
# import time
#
# def make_progress(percent,width=50):
# if percent > 1:percent=1
# show_str=('[%%-%ds]' % width) % (int( percent * width) * '#')
# print('\r%s %s%%' %(show_str,int(percent * 100)),end='')
#
# total_size=1025
# recv_size=0
# while recv_size < total_size:
# time.sleep(0.1) # Simulate 1024 bytes downloaded after a network delay of 0.5
# recv_size+=1024
# # Call the function to print the progress bar to print the progress bar
# percent=recv_size / total_size

#     make_progress(percent)

04 subprocess

# import os
# os.system('tasklist')


import subprocess
import time


obj=subprocess.Popen(
    'tasklist',
    shell=True, #fixed format
    stdout=subprocess.PIPE, #standard output, the correct pipe
    stderr with the parent program =subprocess.PIPE #Standard error output, and the error pipe of the parent program


)
# print(obj)
# stdout_res=obj.stdout.read()
# print(stdout_res.decode('gbk')) #decode byte mode to gbk pattern
# print(stdout_res)


stderr_res1=obj.stderr.read()
print(stderr_res1) #no result
#stderr_res2=obj.stderr.read()
#stderr_res3=obj.stderr.read()
# print(stderr_res1.decode(' gbk'))
# print(stderr_res1)
# print(stderr_res2)
# print(stderr_res3)


# import time
# time.sleep(50)

os. module added:

import os
# print(os.path.normcase('c:/WIndows\\system32\\')   )
结果:c:\windows\system32\
# print(os.path.normcase('c:/WIndows\\system32\\')   )
结果:c:\windows\system32\

Related Posts