天天看點

Python程式設計:xlm檔案讀寫讀取修改删除建立help(ET)

xml檔案增删改查

先引入解析xml文檔的子產品

import xml.etree.ElementTree as ET

tree = ET.parse("data.xml")  # 解析文檔
root = tree.getroot()  # 擷取根節點
print(root)
print(root.tag)      

讀取

# 周遊文檔
for child in root:
    print(child.tag, child.attrib)
    for i in child:
        print(i.tag, i.attrib, i.text)

# 隻周遊year節點
for nood in root.iter("year"):
     print(nood.tag, nood.text)      

修改

for node in root.iter("year"):
    new_year = int(node.text) + 1
    node.text = str(new_year)
    node.set("update_by", "Tom")

tree.write("data.xml") # 修改後需要儲存      

删除

for country in root.findall("country"):
    rank = int(country.find("rank").text)
    if rank >50:
        root.remove(country)

tree.write("data.xml")  # 修改後需要儲存      

建立

new_xml = ET.Element("personlist")

person = ET.SubElement(new_xml, "person", attrib={"enrolled": "yes"})
name1 = ET.SubElement(person, "name")
name1.text = "Tom"
age = ET.SubElement(person, "age", attrib={"checked": "no"})
age.text = "33"
sex = ET.SubElement(person, "sex")
sex.text = 'man'

person2 = ET.SubElement(new_xml, "person", attrib={"enrolled": "no"})
name2 = ET.SubElement(person2, "name")
name2.text = "Jimi"
age2 = ET.SubElement(person2, "age")
age2.text = '19'
sex2 = ET.SubElement(person2, "sex")
sex2.text = "women"

et = ET.ElementTree(new_xml)  # 生成文檔對象
et.write("test.xml", encoding="utf-8", xml_declaration=True)  # 寫入檔案

ET.dump(new_xml)  # 列印生成的格式      

help(ET)

"""
Each Element has a number of properties associated with it:

       'tag' - a string containing the element's name.

       'attrib' - a Python dictionary storing the element's attributes.

       'text' - a string containing the element's text content.

       'tail' - an optional string containing text after the element's end tag.


    class Element(builtins.object)
     |  
     |  append(...)
     |  
     |  clear(...)
     |  
     |  extend(...)
     |  
     |  find(...)
     |  
     |  findall(...)
     |  
     |  findtext(...)
     |  
     |  get(...)
     |  
     |  getchildren(...)
     |  
     |  getiterator(...)
     |  
     |  insert(...)
     |  
     |  items(...)
     |  
     |  iter(...)
     |  
     |  iterfind(...)
     |  
     |  itertext(...)
     |  
     |  keys(...)
     |  
     |  makeelement(...)
     |  
     |  remove(...)
     |  
     |  set(...)

    class ElementTree(builtins.object)

     |  
     |  Methods defined here:
     |  
     |  __init__(self, element=None, file=None)
     |  
     |  find(self, path, namespaces=None)
     |      Find first matching element by tag name or path.
     |      Return the first matching element, or None if no element was found.
     |  
     |  findall(self, path, namespaces=None)
     |      Find all matching subelements by tag name or path.
     |      Return list containing all matching elements in document order.
     |  
     |  findtext(self, path, default=None, namespaces=None)
     |      Find first matching element by tag name or path.
     |      Return the first matching element, or None if no element was found.
     |  
     |  getiterator(self, tag=None)
     |      # compatibility
     |  
     |  getroot(self)
     |      Return root element of this tree.
     |  
     |  iter(self, tag=None)
     |      Create and return tree iterator for the root element.
     |  
     |  iterfind(self, path, namespaces=None)
     |      Find all matching subelements by tag name or path.
     |      Return an iterable yielding all matching elements in document order.
     |  
     |  parse(self, source, parser=None)
     |      Load external XML document into element tree.
     |      Returns the root element of the given source document.
     |  
     |  write(self, file_or_filename, encoding=None, xml_declaration=None, default_namespace=None, method=None, *, short_empty_elements=True)
     |      Write element tree to a file as XML.
     |  ----------------------------------------------------------------------


FUNCTIONS
    Comment(text=None)
        Comment element factory.

    PI = ProcessingInstruction(target, text=None)
        Processing Instruction element factory.

    ProcessingInstruction(target, text=None)
        Processing Instruction element factory.

    SubElement(...)

    XML(text, parser=None)
        Parse XML document from string constant.
        Returns an Element instance.

    XMLID(text, parser=None)
        Parse XML document from string constant for its IDs      
        Returns an (Element, dict) tuple, in which the
        dict maps element id:s to elements.

    dump(elem)
        Write element tree or element structure to sys.stdout.

    fromstring = XML(text, parser=None)
        Parse XML document from string constant.
        Returns an Element instance.

    fromstringlist(sequence, parser=None)
        Parse XML document from sequence of string fragments.
        Returns an Element instance.

    iselement(element)
        Return True if *element* appears to be an Element.

    iterparse(source, events=None, parser=None)
        Incrementally parse XML document into ElementTree.
        Returns an iterator providing (event, elem) pairs.

    parse(source, parser=None)
        Parse XML document into element tree.
        Return an ElementTree instance.

    register_namespace(prefix, uri)
        Register a namespace prefix.

    tostring(element, encoding=None, method=None, *, short_empty_elements=True)
        Generate string representation of XML element.     
        Returns an (optionally) encoded string containing the XML data.

    tostringlist(element, encoding=None, method=None, *, short_empty_elements=True)

"""