gpt4 book ai didi

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

转载 作者:数据小太阳 更新时间:2023-10-29 02:39:08 26 4
gpt4 key购买 nike

我明白了

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com