PyDocs

Nav

Structured Markup Processing Tools

ElementTree.write

  • ElementTree.write(file, encoding='us-ascii', xml_declaration=None, default_namespace=None, method='xml', *, short_empty_elements=True)
Write element tree to a file as XML.

Arguments:
  *file_or_filename* -- file name or a file object opened for
                        writing

  *encoding* -- the output encoding (default: US-ASCII)

  *xml_declaration* -- bool indicating if an XML declaration should
                       be added to the output. If None, an XML
                       declaration is added if encoding IS NOT
                       either of: US-ASCII, UTF-8, or Unicode

  *default_namespace* -- sets the default XML namespace (for
                         "xmlns")

  *method* -- either "xml" (default), "html, "text", or "c14n"

  *short_empty_elements* -- controls the formatting of elements
                            that contain no content.  If True
                            (default) they are emitted as a single
                            self-closed tag, otherwise they are
                            emitted as a pair of start/end tags
Changed in 3.4, 3.8
  • 3.4: Added the short_empty_elements parameter.
  • 3.8: The write() method now preserves the attribute order specified by the user.
1 Example
>>> for country in root.findall('country'):
...     # using root.findall() to avoid removal during traversal
...     rank = int(country.find('rank').text)
...     if rank > 50:
...         root.remove(country)
...
>>> tree.write('output.xml')
from the Python docs
See Also
No see-alsos for xml.etree.ElementTree/ElementTree.write
0 Notes
No notes for ElementTree.write