【问题标题】:Python ElementTree ParseError from iterparse when reaching escape character (XML)到达转义字符(XML)时来自iterparse的Python ElementTree ParseError
【发布时间】:2017-04-18 05:50:21
【问题描述】:

这个问题似乎与this one from 2013 有关,但对我没有帮助。

我即将解析一个大的 (2GB) XML 文件,并计划使用 Python 3.5.2 和 ElementTree 来解析。我是 Python 新手,但在遇到任何转义字符之前它都能正常工作,例如:

<author>Sanjeev Sax&ouml;na</author>

返回:

test.xml
  File "<string>", line unknown
ParseError: undefined entity &ouml;: line 5, column 19enter code here

我的代码如下所示:

import xml.etree.ElementTree as etree
for event, elem in etree.iterparse('test_esc.xml'):
  # do something with the node

解决这个问题的最佳方法是什么?解析未转义的“ö”实际上可以正常工作:

<author>Sanjeev Saxöna</author>

有没有一种简单的方法可以以编程方式对整个 XML 文件进行转义?

【问题讨论】:

  • 这里有答案stackoverflow.com/questions/14744945/…>
  • 谢谢!这个答案对我有帮助。

标签: python xml parsing xml-parsing elementtree


【解决方案1】:

根据 Soulaimane Sahmi 链接的答案的建议,我在 XML 文件中添加了一个内联 DTD。这可能不是目前最好的解决方案,但它目前有效。

【讨论】:

    猜你喜欢
    • 2010-10-13
    • 2021-10-08
    • 1970-01-01
    • 1970-01-01
    • 2016-02-22
    • 2019-06-24
    • 2012-06-30
    • 2020-01-02
    • 1970-01-01
    相关资源
    最近更新 更多