我有一个很小的xml解析python代码片段,它可以使用ElementTree,但不能使用cElementTree。为什么会这样呢?
#!/usr/bin/python3
import sys
import xml.etree.cElementTree as ET
tree = ET.parse(sys.stdin)这就产生了例外情况:
cElementTree.ParseError: no element found: line 1, column 0当它叫成这样
echo "<a><b>c</b></a>" | ./xmltest.py编辑:我刚刚注意到代码片段在python 2.7.2中工作,但在python 3.2.2或3.1.4中不起作用,知道为什么吗?
更新:它似乎在python3.3中被修复了
发布于 2012-03-12 15:08:06
您已经遇到了最近在第14246期中记录的bug。在修复之前,Python3的一个解决方法是将sys.stdin改为byte流,而不是string流:
import sys
import xml.etree.cElementTree as ET
sys.stdin = sys.stdin.detach()
tree = ET.parse(sys.stdin)https://stackoverflow.com/questions/9665558
复制相似问题