python - 元素树.ParseError : reference to invalid character number

标签 python xml elementtree

我明白了

ElementTree.ParseError: reference to invalid character number

当解析包含以下内容作为标记值的 XML 时:locat

我的代码如下:

respXML = httpResponse.content
#also possible respXML = httpResponse.content.decode("utf-8") 
#but both get the same error

#this line throws the error
respRoot = ET.fromstring(respXML)

我怎样才能让我的解析器免受看似无效的字符数字的攻击?

最佳答案

这看起来像 html。先看看是否在输入字符串上使用 html 包。 https://pypi.python.org/pypi/html

>>> import html
>>> test = "locat"
>>> html.unescape(test)
'local'

然后将一些已知的 unicode 字符转换为它们的等效字符。即

“ => "
’ => '
... 

最后将双空格替换为单空格。

因为预先成功解决所有问题会非常麻烦 - 我建议放置特定的异常(exception)并将错误行写入文件。 通过添加更多规则逐一解决输出文件中的每个错误。

祝你好运。

关于python - 元素树.ParseError : reference to invalid character number,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41067475/

相关文章:

xml - 将 xml 从一个文件夹移动到另一个文件夹时出错。字母之间为空

Python ElementTree - 按顺序遍历子节点和文本

python - pip 安装 Pandas : installing dependencies error

python - Django 1.6 中的部分索引

python - VISA协议(protocol)下操作之间等待一段时间正常吗?

python - 在 ET 中搜索子元素

Python 元素树

python - 如何在 Ubuntu 中将 mod_wsgi 安装到特定的 python 版本?

sql-server - SQL 服务器 2012 : Select xml with repeated and ungrouped set of elements

android - 更改 xml 使我的应用程序崩溃