ElementTree documentation error
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.Ele...> 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.Ele...> 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.Ele...>. 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.
Please disregard, I conflated two arguments.
On Dec 30, 2017, at 11:01 AM, Todd Wells <todd@wellshub.com> wrote:
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.Ele...> 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.Ele...> 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.Ele...>. 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.
participants (1)
-
Todd Wells