• ET.parse().getroot()


    参考  ET.parse().getroot() - 云+社区 - 腾讯云

    getroot()

    Returns the root element for this tree.

    iter(tag=None)

    Creates and returns a tree iterator for the root element. The iterator loops over all elements in this tree, in section order. tag is the tag to look for (default is to return all elements).

    iterfind(match, namespaces=None)

    Same as Element.iterfind(), starting at the root of the tree.

    New in version 3.2.

    parse(source, parser=None)

    Loads an external XML section into this element tree. source is a file name or file object. parser is an optional parser instance. If not given, the standard XMLParser parser is used. Returns the section root element.

    write(file, encoding="us-ascii", xml_declaration=None, default_namespace=None, method="xml", *, short_empty_elements=True)

    Writes the element tree to a file, as XML. file is a file name, or a file object opened for writing. encoding 1 is the output encoding (default is US-ASCII). xml_declaration controls if an XML declaration should be added to the file. Use False for never, True for always, None for only if not US-ASCII or UTF-8 or Unicode (default is None). default_namespace sets the default XML namespace (for “xmlns”). method is either "xml", "html" or "text" (default is "xml"). The keyword-only short_empty_elements parameter controls the formatting of elements that contain no content. If True (the default), they are emitted as a single self-closed tag, otherwise they are emitted as a pair of start/end tags.

    The output is either a string (str) or binary (bytes). This is controlled by the encoding argument. If encoding is "unicode", the output is a string; otherwise, it’s binary. Note that this may conflict with the type of file if it’s an open file object; make sure you do not try to write a string to a binary stream and vice versa.

    New in version 3.4: The short_empty_elements parameter.

    This is the XML file that is going to be manipulated:

    1. <html>
    2. <head>
    3. <title>Example pagetitle>
    4. head>
    5. <body>
    6. <p>Moved to <a href="http://example.org/">example.orga>
    7. or <a href="http://example.com/">example.coma>.p>
    8. body>
    9. html>

    Example of changing the attribute “target” of every link in first paragraph:

    1. >>> from xml.etree.ElementTree import ElementTree
    2. >>> tree = ElementTree()
    3. >>> tree.parse("index.xhtml")
    4. <Element 'html' at 0xb77e6fac>
    5. >>> p = tree.find("body/p") # Finds first occurrence of tag p in body
    6. >>> p
    7. <Element 'p' at 0xb77ec26c>
    8. >>> links = list(p.iter("a")) # Returns list of all links
    9. >>> links
    10. [<Element 'a' at 0xb77ec2ac>, <Element 'a' at 0xb77ec1cc>]
    11. >>> for i in links: # Iterates through all found links
    12. ... i.attrib["target"] = "blank"
    13. >>> tree.write("output.xhtml")

  • 相关阅读:
    从零开始学习SLAM
    静态路由———初学
    文本情感倾向查询易语言代码
    基于Vue+SpringBoot的厦门旅游电子商务预订系统 开源项目
    代码随想录 -- day55 --392.判断子序列 、115.不同的子序列
    数位dp。。。
    干货!Python四大常用绘图库,深度解析
    箱包面料裁剪装置机械系统设计
    【TB作品】MSP430G2533,读取dht11,显示到lcd1602显示屏,串口发送到电脑
    数据结构与算法_排序算法_冒泡排序和选择排序
  • 原文地址:https://blog.csdn.net/weixin_36670529/article/details/101985253