[docs] ElementTree documentation error

Todd Wells todd at wellshub.com
Sat Dec 30 14:01:06 EST 2017


I found this error in the documentation (https://docs.python.org/3.7/library/xml.etree.elementtree.html <https://docs.python.org/3.7/library/xml.etree.elementtree.html> and earlier versions):


xml.etree.ElementTree.tostring(element, encoding="us-ascii", method="xml", *, short_empty_elements=True) <https://docs.python.org/3.7/library/xml.etree.elementtree.html#xml.etree.ElementTree.tostring>
Generates a string representation of an XML element, including all subelements.  element is an Element <https://docs.python.org/3.7/library/xml.etree.elementtree.html#xml.etree.ElementTree.Element> instance.  encoding [1] <https://docs.python.org/3.7/library/xml.etree.elementtree.html#id5> is the output encoding (default is US-ASCII). Use encoding="unicode" to generate a Unicode string (otherwise, a bytestring is generated). method is either "xml", "html" or "text" (default is "xml"). short_empty_elements has the same meaning as in ElementTree.write() <https://docs.python.org/3.7/library/xml.etree.elementtree.html#xml.etree.ElementTree.ElementTree.write>. Returns an (optionally) encoded string containing the XML data.

The error here is the method value “text” is not a valid argument as the docs state. The correct value is “us-ascii”. From an error I encountered:

Traceback (most recent call last):
  File "/Users/todd.wells/Dropbox/thePlatform SA/aws/lambda-ingest-proxy/modify-title-handler/test_modify_title_handler.py", line 11, in test_modify_title
    modified = handler.lambda_handler(event, None)
  File "/Users/todd.wells/Dropbox/thePlatform SA/aws/lambda-ingest-proxy/modify-title-handler/modify_title_handler.py", line 17, in lambda_handler
    event['body'] = ET.tostring(root, encoding='text', method='xml')
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/xml/etree/ElementTree.py", line 1135, in tostring
    short_empty_elements=short_empty_elements)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/xml/etree/ElementTree.py", line 759, in write
    with _get_writer(file_or_filename, enc_lower) as write:
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/contextlib.py", line 82, in __enter__
    return next(self.gen)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/xml/etree/ElementTree.py", line 831, in _get_writer
    newline="\n")
LookupError: unknown encoding: text

Using “us-ascii” instead of “text” works.






-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/docs/attachments/20171230/a3047a26/attachment-0001.html>


More information about the docs mailing list