Parsing XML documentation has a gotcha for noobies...

Bug #1272947 reported by xtian
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
lxml
Fix Released
Low
scoder

Bug Description

As a Python Noob, I hesitate to mention this; however, there seems to be a strange gotcha in the [documentation](http://lxml.de/parsing.html#parsers) which can quickly be remedied.

Learning the parsing operations I was simply copying and pasting these instructions,
[code]
>>> from lxml import etree
[...]
>>> xml = '<a xmlns="test"><b xmlns="test"/></a>'
>>> root = etree.fromstring(xml)
>>> etree.tostring(root)
b'<a xmlns="test"><b xmlns="test"/></a>'
[...]
>>> tree = etree.parse(StringIO(xml))
>>> etree.tostring(tree.getroot())
b'<a xmlns="test"><b xmlns="test"/></a>'
[/code]

first I got the error,
    At NameError: name 'StringIO' is not defined

I recognized the StringIO module likely needed to be imported. But the fix
    >>> from StringIO import StringIO

is not described in the documentation and involves a gotcha a Noob may not expect and other Python programmers admit is [strange] (https://mail.python.org/pipermail/python-ideas/2009-February/003081.html).

----
>>> print("%-20s: %s" % ('lxml.etree', etree.LXML_VERSION))
lxml.etree : (3, 3, 0, -95)
>>> print("%-20s: %s" % ('libxml used', etree.LIBXML_VERSION))
libxml used : (2, 9, 1)
>>> print("%-20s: %s" % ('libxml compiled', etree.LIBXML_COMPILED_VERSION))
libxml compiled : (2, 9, 1)
>>> print("%-20s: %s" % ('libxslt used', etree.LIBXSLT_VERSION))
libxslt used : (1, 1, 28)
>>> print("%-20s: %s" % ('libxslt compiled', etree.LIBXSLT_COMPILED_VERSION))
libxslt compiled : (1, 1, 28)
---

Revision history for this message
scoder (scoder) wrote :

StringIO is a bit tricky because it was moved around in Py3. But it's generally available as "io.StringIO" these days. I added that to the docs. Thanks for the suggestion.

Changed in lxml:
assignee: nobody → scoder (scoder)
importance: Undecided → Low
milestone: none → 3.3
status: New → Fix Committed
Revision history for this message
scoder (scoder) wrote :

Docs updated in lxml 3.3.2.

Changed in lxml:
status: Fix Committed → Fix Released
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.