Python16_day06 "class, re module, subprocess module, XML module, shelve module"

Source: Internet
Author: User

One, shelve module

  

1 Importshelve2 3 #based on the Pickle module,4 5D = Shelve.open ('shelve_test')6 7 8 classTest (object):9     def __init__(self, n):TenSELF.N =N One  AT1 = Test (123) -T2 = Test (456) -name = ['Alex','Rain','Test'] thed['Test'] =name -d['T1'] =T1 -d['T2'] =T2 -  +D.close ()

Second, XML module

1. Increase, delete, change, check

ImportXml.etree.ElementTree as Ettree= Et.parse ("List.xml") Root=tree.getroot ()Print(Root.tag)#Enquiry## Traverse XML document#For child in root:#print (Child.tag, child.attrib)#For I in the child :#print (I.tag, i.text)#Traverse only the year node#For node in Root.iter (' year '):#print (Node.tag, node.text)#Modify#For node in Root.iter (' year '):#new_year = Int (node.text) + 1#node.text = str (new_year)#Node.set ("updated", "yes")##tree.write ("Xmltest.xml")#Delete Node#For country in Root.findall (' Country '):#rank = Int (country.find (' rank '). Text)#if rank >:#root.remove (country)##tree.write (' Output.xml ')
View Code

2. Create

1 ImportXml.etree.ElementTree as ET2 3 #Root4New_xml = ET. Element ("NameList")5 6 7Name = ET. Subelement (New_xml,"name", attrib={"enrolled":"Yes"})8Age = ET. subelement (Name," Age", attrib={"checked":"No"})9Sex = ET. subelement (Name,"Sex")TenSex.text =' -' OneName2 = ET. Subelement (New_xml,"name", attrib={"enrolled":"No"}) AAge = ET. Subelement (Name2," Age") -Age.text =' +' -  the #Generating Document Objects -ET =ET. ElementTree (New_xml) -Et.write ("Test.xml", encoding="Utf-8", xml_declaration=true)
View Code

Three, Shutil module

1 ImportShutil2 #http://www.cnblogs.com/wupeiqi/articles/4963027.html3 4 #Copy fileobj5 #f1 = open (' Access.log ')6 #F2 = open ("Access1.log", ' W ')7 #shutil.copyfileobj (F1, F2, length=1024)8 9 #Copy FileTen #shutil.copyfile (' Access.log ', ' Access2.log ') One  A #Copy permissions only. Content, group, user are not changed - #Shutil.copymode () -  the #information about the status of the copy, including: mode bits, Atime, mtime, flags. Content unchanged - #Shutil.copystat () -  - #Copy Directory
View Code

Four, subprocess module

1 Importsubprocess2 3res = subprocess. Popen ("pwd", Shell=true, stdout=subprocess. PIPE, Stderr=subprocess. PIPE, cwd="/")4 Print(Res.stdout.read ())5 6 #Res.poll ()7 #res.terminate ()8 #res.wait ()9 TenSubprocess.getstatusoutput ("ls")
View Code

Five, re module

ImportRe#match from scratch, rarely usedRe.match ("\d+","341221")#Match onceRe.search ("\d+","341221")#Match multiple timesRe.findall ("\d+","341221")#split with commasRe.split (",","341,221")#match to the replacement, the default is to override all, count specified number of times.Re.sub ("\d{4}","1995","1399,2017", Count=1)#Re. I (ignoring case)#Print (Re.search ("[A-Z]", "Alex", Flags=re. I))#Re. M (matches multiple lines)#Print (Re.search ("^is", "My Name\nis Alex", Flags=re. M))#Re. S (multi-line match together)#Print (Re.search (". +", "My \nname", Flags=re. S))
View Code

Vi.. Configparser Module

1 mport Configparser2  3Config =Configparser. Configparser ()4config["DEFAULT"] = {'Serveraliveinterval':' $',5                       'Compression':'Yes',6                      'CompressionLevel':'9'}7  8config['bitbucket.org'] = {}9config['bitbucket.org']['User'] ='HG'Tenconfig['topsecret.server.com'] = {} OneTopsecret = config['topsecret.server.com'] Atopsecret['Host Port'] ='50022'     #mutates the parser -topsecret['ForwardX11'] ='No'  #same here -config['DEFAULT']['ForwardX11'] ='Yes' theWith open ('Example.ini','W') as ConfigFile: -Config.write (ConfigFile)

Vii.. Class

Http://www.cnblogs.com/wupeiqi/p/4493506.html

Http://www.cnblogs.com/wupeiqi/p/4766801.html

Python16_day06 "class, re module, subprocess module, XML module, shelve module"

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.