Created
October 21, 2010 17:26
-
-
Save gelim/638908 to your computer and use it in GitHub Desktop.
lxml.etree & StringIO strangeness
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import StringIO, lxml.etree | |
xmldata="<root>data</root>" | |
buffer=StringIO.StringIO(xmldata) | |
for event, element in lxml.etree.iterparse(buffer): | |
print "%s %s %s" % (event, element.tag, element.text) | |
buffer.close() | |
buffer=StringIO.StringIO() | |
buffer.write(xmldata) | |
buffer.seek(0) # YAY! thanks nosklo (#python@freenode) | |
for event, element in lxml.etree.iterparse(buffer): | |
print "%s %s %s" % (event, element.tag, element.text) | |
buffer.close() |
your second code doesn't seek back to beginning. Use .seek(0)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
By filling on the second part the StringIO via .write() makes lxml.etree.iterparse throw an execption, why ?