Python base day-13[module: Re,subprocess not finished]

Source: Internet
Author: User

Re (cont.):

The re defaults to greedy mode.

Greedy mode: Matches the string as long as possible when matching matches are met.

ImportRes='ASKLDLAKSDABCCCCCCCCASDABCCCALSDACBCCCACBCCCABCCC'Res= Re.findall ('abc+', s)Print(res) Res= Re.findall ('abc+?', s) #在规则后面加? To cancel the greedy mode. Print(res) execution Result: D:\Python\Python36-32\python.exe e:/python/day-15/3213. py['ABCCCCCCCC','ABCCC','ABCCC']['ABC','ABC','ABC']process finished with exit code 0

Re modules are commonly used in the following ways:

Re.split (): A string-like split command but more powerful than the split string.

ImportRes='ASKLDLAKSDAB8CCCCC.CCCAS8DABC8CC.ALSDACBCCCAC.CCCAB8CCC'Res= Re.split ('\d', s)Print(res) Res= Re.split ('(\d+)', s) #加 () to preserve the separatorPrint(res) execution Result: D:\Python\Python36-32\python.exe e:/python/day-15/3213. py['Askldlaksdab','Ccccc.cccas','DABC','Cc.alsdacbcccac.cccab','CCC']['Askldlaksdab','8','Ccccc.cccas','8','DABC','8','Cc.alsdacbcccac.cccab','8','CCC']process finished with exit code 0

Re.sub (): Similar to replace replacement operation.

Import'askldlaksdab8ccccc.cccas8dabc8cc.alsdacbcccac.cccab8ccc '  = re.sub ('abc+','123', s)print  (res) execution Result: D:\Python\Python36-32\python.exe e:/python/day-15/3213. Pyaskldlaksdab8ccccc.cccas8d1238cc.alsdacbcccac.cccab8cccProcess finished with exit code 0

Re.compile (): Compiling

ImportRes='ASKLDLAKSDAB8CCCCC.CCCAS8DABC8CC.ALSDACBCCCAC.CCCAB8CCC'obj= Re.compile ('\d+') #定义一个对象对应的编译规则res=Obj.findall (s) #调用处理Print(res) execution Result: D:\Python\Python36-32\python.exe e:/python/day-15/3213. py['8','8','8','8']process finished with exit code 0

A small reptile is practicing (climbing the campus network)

ImportRequests,re,jsonurl='http://www.xiaohuar.com/2014.html' #校花排行榜top120defreq (): Req_str=requests.get (URL)#print (' encoding ', req_str.encoding)    returnReq_str.textdefrun (): HTML=req () HTML= Html.encode ('Latin-1'). Decode ('GBK')    #print (HTML)obj = Re.compile ('<div class= "Top-title" > (. *?) </div>.*?<div class= "title" >.*?target= "_blank" > (. *?) </a></span></div>', Re. S) #匹配top排名序号和姓名学校 Res=obj.findall (HTML)returnResdic={}res=Run () forXinchRes:dic[x[0]]=x[1]data=json.dumps (DIC) #序列化with open ('Xiaohua.json','a', encoding='Utf-8') as F:f.write (data) with open ('Xiaohua.json','R', encoding='Utf-8') as F:data=json.load (f) #反序列化Print(data)

Subprocess:

The Subprocess module allows a process to create a new child process, connect to the stdin/stdout/stderr of the child process through a pipeline, and get the return value of the child process.

Importsubprocesss= subprocess. Popen ('dir', shell=true,stdout=subprocess. PIPE)Print(S.stdout.read (). Decode ('GBK')) Results of execution: D:\Python\Python36-32\python.exe e:/python/day-15/3213the volume in. PY Drive E is not labeled. The serial number of the volume is 383D-453A E:\Python\DAY-15the directory2017/06/27 19:52 <DIR>          .2017/06/27 19:52 <DIR>          ..2017/06/27 19:52 338 3213. PY2017/06/27 19:47 778tmp.py2017/06/27 19:25 9,146Xiaohua.json3 Files 10,262bytes2 Listings 117,877,260,288Free bytes process finished with exit code 0

Python base day-13[module: Re,subprocess not finished]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.