>>> from xml.etree.ElementTree import Element,ElementTree
Element is the node element
ElementTree is composed of Element
1. Create an element
>>> e = Element( " Data " ) #Create an element, pass in a string <Data> is the name of the head >>> e.set( ' name ' , ' abc ' ) # The attributes of this element, get gets the property, set sets the property. The attribute is 'name' and the value is abc
>>> help(Element.set) Help on method set in module xml.etree.ElementTree: set(self, key, value) unbound xml.etree.ElementTree.Element method
>>> from xml.etree.ElementTree import tostring #Displayed as an XML element and converted into a string >>> tostring(e) ' <Data name="abc" /> '
All content of #XML elements should be strings and should not be other data types
>>> e.text = 123 >>> tostring(e) Traceback (most recent call last): File "<pyshell#13>", line 1, in <module> tostring(e) File "C:\Python27\lib\xml\etree\ElementTree.py", line 1126, in tostring ElementTree(element).write(file, encoding, method=method) File "C:\Python27\lib\xml\etree\ElementTree.py", line 820, in write serialize(write, self._root, encoding, qnames, namespaces) File "C:\Python27\lib\xml\etree\ElementTree.py", line 937, in _serialize_xml write(_escape_cdata(text, encoding)) File "C:\Python27\lib\xml\etree\ElementTree.py", line 1075, in _escape_cdata _raise_serialization_error(text) File "C:\Python27\lib\xml\etree\ElementTree.py", line 1052, in _raise_serialization_error "cannot serialize %r (type %s)" % (text, type(text).__name__) TypeError: cannot serialize 123 (type int)
>>> e.text = '123' >>> tostring(e) '<Data name="abc">123</Data>'
2. Add child elements to an element
>>> e2 = Element( ' Row ' ) # create an element >>> e3 = Element( ' Open ' ) >>> e3.text = ' 8.80 ' >>> e2.append(e3) # add e3 to e2 becomes its child element >>> tostring(e2) ' <Row><Open>8.80</Open></Row> '
>>> help(Element.append) Help on method append in module xml.etree.ElementTree: append(self, element) unbound xml.etree.ElementTree.Element method
#Method to delete an assigned element attribute
>>> e.text '123' >>> e.text = None >>> tostring(e) '<Data name="abc" />' >>> e.append(e2) >>> tostring(e) '<Data name="abc"><Row><Open>8.80</Open></Row></Data>'
3. Write the string in XML format to the file
(1) Create an ElementTree
>>> et = ElementTree(e) >>> et <xml.etree.ElementTree.ElementTree object at 0x027166B0>
(2) Use the write method of the ElementTree object
>>> et.write(r " C:\video\python efficient practice skills notes\6 data encoding and processing related topics\6-4.xml " )
Example: Convert the csv file bank-databak.csv in 6-1 into an xml file, the script file is
import csv from xml.etree.ElementTree import Element, ElementTree “”” Beautify the xml file, that is, make the output xml file have a reasonable indentation format “”” def pretty(e,level = 0): if(len(e)>0): e.text = '\n' + '\t' * (level+1) for child in e: pretty(child,level+1) child.tail = child.tail[:-1] e.tail = '\n' +'\t'*level “”“ CSV file to XML file, the parameter is the path of the CSV file ”“” def CsvToXml(fname): with open(fname) as f: #Open the CSV file reader = csv.reader(f) #Read the CSV file head = reader.next() #Get the header of the CSV file root = Element( ' Data ' ) #Create XML an element as the root node for row in reader: #Iterate over the header of the CSV eRow = Element( ' Row ' ) # #Create an element Row root.append(eRow) #Add this Row element to the root as a child node for tag, text in zip(head,row): Iterates each item of the header and each item of each row of the CSV file at the same time e = Element(tag) #Take the item of the CSV header as an element of the XML file e.text = text #Take the content of each item of each line in the CSV file as the value of each element of the XML. eRow.append(e) #Add this element of e to Row as a child node pretty(root) #Beautify the XML file return ElementTree(root) #Return an EleMentTree object #Call the conversion function and return an ElementTree object et = CsvToXml(r ' C:\video\python efficient practice skills notes\6 topics related to data encoding and processing\bank-databak.csv ' ) #Write the returned ElementTree object to the file et.write(r ' C:\video\python efficient practice skills notes\6 topics related to data encoding and processing\bank-databak.xml ' )
result: