- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我有一个小的 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 中无效,知道为什么吗?
更新:似乎在 python 3.3 中修复了
最佳答案
您遇到了最近在 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)
关于python - ElementTree/cElementTree 区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9665558/
我正在尝试从字符串构建ElementTree。当我执行以下操作时(如 Python ElementTree: Parsing a string and getting ElementTree inst
我正在使用 elementtree.ElementTree.iterparse 来解析大型 (371 MB) xml 文件。 我的代码基本上是这样的: outf = open('out.txt', '
有没有办法在 elementtree.ElementTree 中忽略标记名称中的 XML 命名空间? 我尝试打印所有 technicalContact 标签: for item in root.get
我使用 xml.etree.elementtree.Element 创建了一个 XML 文档,并想使用 ElementTree.write() 函数打印它但是出来的声明标签是 虽然我需要用双引号引起
这个问题已经有答案了: What is the best way to remove accents (normalize) in a Python unicode string? (14 个回答)
我想为此处元素国家/地区新加坡旁边的元素创建子元素。 假设我的 test.xml 文件如下所示 2008 141100
我正在使用ElementTree加载一系列 XML 文件并解析它们。解析文件时,我将从其中获取一些数据(标题和文本段落)。然后我需要获取一些存储在 XML 中的文件名。它们包含在名为 ContentI
我必须将多个 XML 文件合并为一个。此外,新文件的结构也不同。这是我的“旧”结构: 1
我正在解析一个 xml 文件:http://pastebin.com/fw151jQN我希望在副本中读取它的大部分内容并将其写入一个新文件,其中一些已修改,很多未修改,还有很多被忽略。作为初始阶段,我
这是 XML: TARGET_NAME_1 5 a string goes here TARGET_NA
from lxml import etree from xml.etree.ElementTree import Element, SubElement, dump listing = Element
当涉及到模块/库时,为了可读性,我喜欢在 python 中使用完整的命名空间。我想知道为什么这对 xml 库不起作用。我认为 import xml 还将导入 etree 和命名空间中的所有其他内容。至
这里是 Python 菜鸟。想知道删除所有 updated 属性值为 true 的“profile”标签的最干净、最好的方法是什么。 我已经尝试了下面的代码,但它抛出了:SyntaxError("ca
尝试从 xml 文档中删除元素时出现以下错误。“ValueError: list.remove(x): x 不在列表中”这是代码,错误发生在删除的行上。 import xml.etree.Elemen
所以我必须编写一个“重复检查器”来比较两个 XML,看看它们是否相同(包含相同的数据)。现在因为它们来自同一个类并且是从 XSD 结构中生成的,所以内部元素的顺序很可能是相同的。 我能想到的进行重复检
我有一个 XML 文档,我正在使用 ElementTree 阅读和附加该文档。这有多个命名空间声明。据我所知,ElementTree 只允许声明一个全局命名空间: ET.register_namesp
从这里开始: stuff
我是 ElementTree 的新手。我正在尝试获取 来自 XML 响应的值。 以下代码对我不起作用。如何提取 中的值?我不确定号码在哪里 53是从这里来的。 ... r = req
以下代码: import xml.etree.ElementTree as ET xml = '''\ ''' root = ET.fromstring(xml)
我无法控制我获得的 XML 的质量。在某些情况下是: ... 在其他方面我得到: ... 我想我也应该处理 ... 整个架构都是相同的,我只需要一个解析器来处理它。我该如何处理所有这些
我是一名优秀的程序员,十分优秀!