【问题标题】:ElementTree / cElementTree difference?ElementTree / cElementTree 的区别?
【发布时间】:2012-03-28 18:19:14
【问题描述】:

我有一个小型 xml-parsing python sn-p 可以与 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

编辑:我刚刚注意到 sn-p 在 python 2.7.2 中有效,但在 python 3.2.2 或 3.1.4 中无效,知道为什么吗?

更新:似乎在 python 3.3 中已修复

【问题讨论】:

  • 你见过this SO问题吗?您的案例是 unicode 问题吗?
  • 虽然这似乎是相关的,但我认为我不能用 utf8 编码“重新打开” sys.stdin,对吧?
  • stackoverflow.com/questions/2351694/…有类似的问题吗?

标签: python xml python-3.x elementtree celementtree


【解决方案1】:

您遇到了最近记录在 Issue 14246 中的错误。在修复之前,Python 3 的一种解决方法是将sys.stdin 更改为byte 流而不是string 流:

import sys
import xml.etree.cElementTree as ET

sys.stdin = sys.stdin.detach()
tree = ET.parse(sys.stdin)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多