Difference ElementTree / cElementTree?

I have a small python snippet processing xml that works with ElementTree but not with cElementTree. Why is this?

#!/usr/bin/python3 import sys import xml.etree.cElementTree as ET tree = ET.parse(sys.stdin) 

an exception occurs:

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

when he is called like that

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

EDIT: I just noticed that the fragment 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

+4
source share
1 answer

You have encountered a bug recently reported in Issue 14246 . Until this is fixed, one way around Python 3 is to change the sys.stdin of the byte stream, not the string stream:

 import sys import xml.etree.cElementTree as ET sys.stdin = sys.stdin.detach() tree = ET.parse(sys.stdin) 
+4
source

Source: https://habr.com/ru/post/1400970/


All Articles