From Xml.etree import ElementTree as ET
Tree = Et.parse (' Xo.xml ')
root = Tree.getroot ()
For child in Root:
Print (Child.tag,child.attrib)
For Gradechild in child:
Print (Gradechild.tag, gradechild.text)
Open file, read XML content
From Xml.etree import ElementTree as ET
Str_xml = open (' Xo.xml ', ' R '). Read ()
# Parse a string into an XML special object, root refers to the root node of the XML file
root = ET. XML (Str_xml)
############ Operation ############
# Top Level Label
Print (Root.tag)
# Loop all the year nodes
For node in Root.iter (' year '):
# Increment the contents of the year node by one
new_year = Int (node.text) + 1
Node.text = str (new_year)
# Set Properties
Node.set (' name ', ' Alex ')
Node.set (' Age ', ' 18 ')
# Delete Properties
Del node.attrib[' name ']
############ Saving Files ############
Tree = ET. ElementTree (Root)
Tree.write ("Newnew.xml", encoding= ' Utf-8 ')
from Xml.etree import elementtree as ET
tree = et.parse (' Xo.xml ')
root = Tree.getroot ()
Ele = ET. Element (' Alex ', {' K1 ': ' v1 '})
Ele.text = "I am Content"
Root.append (ele)
Tree.write ("New.xml", encoding= "Utf-8
# list
# root_list = [
# []
#]
#
# list ==> class name
# class name () > created an object
# ele = List ()
# e = Dict ()
# el = Element ()
# root_list.append (ele)
from xml.etree import elementtree as et
# Create root node
root = et. Element ("Famliy")
# Create node big son
Son1 = ET. Element (' son ', {' name ': ' Child 1 '})
# Create the youngest son
Son2 = ET. Element (' son ', {' name ': ' Child 2 '})
# Create two grandchildren in the older son
Grandson1 = ET. Element (' grandson ', {' name ': ' Child One '})
Grandson2 = ET. Element (' grandson ', {' name ': ' Child '})
Son1.append (grandson1)
Son1.append (grandson2)
# To add a son to the root node
Root.append (Son1)
Root.append (son1)
tree = ET. ElementTree (Root)
Tree.write (' Oooo.xml ', encoding= ' utf-8 ', Xml_declaration=true, Short_empty_elements=false)
def prettify (elem):
"" Converts the node to a string and adds indentation.
""
Rough_string = et.tostring (elem, ' utf-8 ')
reparsed = minidom.parsestring (rough_string)
Re Turn reparsed.toprettyxml (indent= "\ t")
# creates the root node
root = ET. Element ("Famliy")
# Create the big son
# son1 = ET. Element (' son ', {' name ': ' Child 1 '})
Son1 = root.makeelement (' son ', {' name ': ' Child 1 '})
# Create the youngest son
# son2 = ET. Element (' son ', {' name ': ' Child 2 '})
Son2 = root.makeelement (' son ', {"name": ' Child 2 '})
# Create two grandchildren in the older son
# grandson1 = BTI Element (' grandson ', {' name ': ' Child One '})
Grandson1 = son1.makeelement (' grandson ', {' name ': ' Child One '})
# grandson2 = ET. Element (' grandson ', {' name ': ' Child '})
Grandson2 = son1.makeelement (' grandson ', {' name ': ' Child '})
Son1.append (Grandson1)
Son1.append (grandson2)
# Add your son to the root node
Root.append (Son1)
Root.append (son1)
Raw_str = Prettify (root)
F = open ("Xxxoo.xml", ' W ', encoding= ' utf-8 ')
F.write (raw_str)
F.close ()
Import ZipFile
# compression
# z = zipfile. ZipFile (' Laxi.zip ', ' a ')
# z.write (' Newnew.xml ')
# z.write (' Xo.xml ')
# Z.close ()
# Unzip
z = zipfile. ZipFile (' Laxi.zip ', ' R ')
# Z.extractall ()
# for item in Z.namelist ():
# Print (Item,type (item))
Z.extract ("New.xml")
Z.close ()
Import Tarfile
# compression
# tar = Tarfile.open (' Your.tar ', ' W ')
# tar.add (' s1.py ', arcname= ' ssss.py ')
# tar.add (' s2.py ', arcname= ' cmdb.py ')
# Tar.close ()
# Unzip
tar = Tarfile.open (' Your.tar ', ' R ')
# Tar.extractall () # can set the decompression address
# for item in Tar.getmembers ():
# Print (Item,type (item))
obj = Tar.getmember ("ssss.py")
Tar.extract (obj)
Tar.close ()
Class SQLHelper:
def __init__ (self, a1, A2, A3):
Print (' Auto-execute init ')
self.hhost = A1
Self.uuserane = A2
SELF.PWD = A3
Self.create (' SSS ')
def fetch (self, SQL):
# Connect Data
Print (Self.hhost)
Print (Self.uuserane)
Print (SELF.PWD)
Print (SQL)
def create (self, SQL):
Pass
def remove (self, nid):
Pass
def modify (self, name):
Pass
Obj1 = SQLHelper (' c1.salt.com ', ' Alex ', 123)
# obj2 = SQLHelper (' c2.salt.com ', ' alex1 ', 12333)
# obj1.create ()
Class C1:
def __init__ (self,name,obj):
Self.name = Name
Self.obj = obj
Class C2:
def __init__ (self,name,age):
Self.name = Name
Self.age = Age
Def show (self):
Print (Self.name)
Return 123
Class C3:
def __init__ (self, A1):
Self.money = 123
SELF.AAA = A1
c2_obj = C2 (' AA ', 11)
# c2_obj is a C2 type
#-name = "AA"
#-age = 11
C1_obj = C1 ("Alex", C2_obj)
# c1_obj is a C1 type
#-Name = "Alex"
#-obj = C2_obj
C3_obj = C3 (C1_obj)
# Use C3_obj to perform the Show method
ret = C3_obj.aaa.obj.show ()
Print (ret)
"""
Class F1: # parent class, base class
Def show (self):
Print (' show ')
def foo (self):
Print (Self.name)
Class F2 (F1): # Subclass, Derived class
def __init__ (self, name):
Self.name = Name
def bar (self):
Print (' Bar ')
Def show (self):
Print (' F2.show ')
obj = F2 (' Alex ')
# obj.show ()
Obj.foo ()
"""
# obj = F2 ()
# Obj.bar ()
# obj.show ()
# obj = F1 ()
# Obj.bar ()
# class S1:
#
# def F1 (self):
# Self. F2 ()
#
# def F2 (self):
# Pass
#
# class S2 (S1):
#
# def F3 (self):
# Self. F1 ()
#
# def F2 (self):
# Pass
#
# obj = S2 ()
# obj. F3 ()
# obj = S1 ()
# obj. F1 ()
Class c_2:
def f2 (self):
Print (' C-2 ')
Class C_1 (c_2):
def f2 (self):
Print (' C-1 ')
Class C0 (c_2):
def F1 (self):
Print (' C0 ')
Class C1 (C0):
def F1 (self):
Print (' C1 ')
Class C2 (c_1):
def f2 (self):
Print (' C2 ')
Class C3 (C1,C2):
def f3 (self):
Pass
obj = C3 ()
OBJ.F2 ()
1. Python function + object-oriented
2, functional programming, object-oriented programming implementation: The function of sending mail
# function
def mail (email, message):
Print ("Go to the Hair Bar")
Return True
Mail ("[email protected]", "good Person")
Object-Oriented: Class, Object
Class Foo:
# method
Def mail (self, email, message):
Print (' Go to the Hair bar ')
Return True
# call
1. Create object, class name ()
obj = Foo ()
2, through the object to execute the method
Obj.mail ("[Email protected]", "good Person")
3. Classes and objects
A. Creating a class
Class Name:
def method Name (self,xxxx):
Pass
B. Creating an Object
Object = Class name ()
C. Methods of implementing objects by object
Object. Method Name (123)
4.
Function Type:
def fetch (host, username, password, sql):
Pass
DEF create (host, username, password, sql):
Pass
def remove (host, username, password, nid):
Pass
def modify (host, username, password, name):
Pass
...
Fetch (...)
Object oriented
Class SQLHelper:
def fetch (self, SQL):
Pass
def create (self, SQL):
Pass
def remove (self, nid):
Pass
def modify (self, name):
Pass
Obj1 = SQLHelper ()
Obj1.hhost = "C1.salt.com"
Obj1.uuserane = "Alex"
Obj1.pwd = "123"
Obj1.fetch ("SELECT * from A")
Obj2 = SQLHelper ()
Obj2.hhost = "C2.salt.com"
Obj2.uuserane = "Alex"
Obj2.pwd = "123"
Obj2.fetch ("SELECT * from A")
When does ========> use object-oriented? When some functions have the same parameters, object-oriented methods can be used to encapsulate the value of the parameter once to the object, and then to the object to take a value
5, Self is what ghost?
Self is a parameter that Python automatically assigns values to
That object executes the method, and self is who.
Obj1.fetch (' Selec ... ') self=obj1
Obj2.fetch (' Selec ... ') self=obj2
6. Construction method
Class has a special method __init__, the class () is automatically executed
7. Object-oriented
Three main features: encapsulation, inheritance, polymorphism
1. Python function + object-oriented