Question

I have a small xml-parsing python snippet that works with ElementTree, but not with cElementTree. Why is that?

#!/usr/bin/python3

import sys
import xml.etree.cElementTree as ET

tree = ET.parse(sys.stdin)

this raises the exception:

cElementTree.ParseError: no element found: line 1, column 0

when its called like this

echo "<a><b>c</b></a>" | ./xmltest.py

EDIT: I just noticed that the snippet works in python 2.7.2, but not in python 3.2.2 or 3.1.4, any idea why?

Update: It seems to be fixed in python 3.3

Was it helpful?

Solution

You've run into the bug recently documented in Issue 14246. Until it is fixed, one workaround for Python 3 is to change sys.stdin to be a byte stream rather than a string stream:

import sys
import xml.etree.cElementTree as ET

sys.stdin = sys.stdin.detach()
tree = ET.parse(sys.stdin)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top